当用户从 DropDownList 选择不同的选项时如何更改内容
How to Change Content When User Selected Different Option From DropDownList
我有一个下拉列表供用户 select 他们想要从 gridview 检索的项目。 (见下文:)
假设首先加载页面时,下拉列表 "Item1" 显示为默认选项。所以首先当页面加载时,我应该根据 ListItem 文本而不是值检索 Item1 内容,并在网页上显示 Item1 内容。如果用户点击了不同的选项(例如Item2),那么我会根据ListItem Text检索Item2的内容,并在网页上显示Item2的内容,等等..
下拉列表代码:
<asp:Dropdownlist ID="DropDownList1" runat="server" Width="136px" AutoPostBack="true" OnSelectedIndexChanged="jobRun_SelectedIndexChanged">
</asp:Dropdownlist>
DropDownList_SelectedIndexChanged代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex >= 0)
{
if (!string.IsNullOrEmpty(1stListBox.SelectedValue) && !string.IsNullOrEmpty(2ndListBox.SelectedValue) && !string.IsNullOrEmpty(3rdListBox.SelectedValue)
&& !string.IsNullOrEmpty(4thListBox.SelectedValue) && !string.IsNullOrEmpty(5thListBox.SelectedValue))
{
1stListBox_LABEL.Text = string.Empty;
2ndListBox_LABEL.Text = string.Empty;
3rdListBox_LABEL.Text = string.Empty;
4thListBoxLABEL.Text = string.Empty;
5thListBox_LABEL.Text = string.Empty;
BindDropDownList1();
}
else
{
if (string.IsNullOrEmpty(1stListBox.SelectedValue))
{
1stListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
1stListBox_LABEL.Text = string.Empty;
}
if (string.IsNullOrEmpty(1stListBox.SelectedValue))
{
1stListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
1stListBox_LABEL.Text = string.Empty;
}
if (2ndListBox.SelectedValue == String.Empty)
{
2ndListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
2ndListBox_LABEL.Text = string.Empty;
}
if (3rdListBox.SelectedValue == String.Empty)
{
3rdListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
3rdListBox_LABEL.Text = string.Empty;
}
if (4thListBox.SelectedValue == String.Empty)
{
4thListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
4thListBox.Text = string.Empty;
}
}
AjaxControlToolkit.TabContainer container = new AjaxControlToolkit.TabContainer();
container.ID = "TabContainer";
container.EnableViewState = false;
container.Tabs.Clear();
container.Height = Unit.Pixel(2000);
container.Width = Unit.Pixel(1200);
container.Tabs.AddAt(0, GetManualTab());
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ID = "HEATMAP";
img.Width = 40;
img.Height = 300;
img.ImageUrl = "Image/HEATMAP.png";
string selectedValue= DropDownList1.SelectedItem.ToString();
if (1stListBox.SelectedValue == "Select All")
{
foreach (ListItem item in 1stListBox.Items)
{
if (item.Value.Equals("1"))
{
continue;
}
Label tabContent = new Label();
tabContent.ID += item.Value;
tabContent.Text += item.Value;
string itemSelected = 1stListBox.SelectedItem.ToString();
TextBox1.Text = tabContent.Text.ToString();
DataTable tg = new DataTable();
DataRow dr;
tg.Columns.Add(new DataColumn("DATE"));
tg.Columns.Add(new DataColumn("STATUS", typeof(string)));
tg.Columns.Add(new DataColumn("TITLE", typeof(string)));
tg.Columns.Add(new DataColumn("NAME", typeof(string)));
tg.Columns.Add(new DataColumn("MAX", typeof(int)));
tg.Columns.Add(new DataColumn("MIN", typeof(int)));
tg.Columns.Add(new DataColumn("AVG", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE25", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE50", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE75", typeof(int)));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[2].Text == tabContent.Text.ToString() && gvr.Cells[0].Text == selectedValue.ToString())
{
dr = tg.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["STATUS"] = gvr.Cells[1].Text;
dr["TITLE"] = gvr.Cells[2].Text;
dr["NAME"] = gvr.Cells[3].Text;
dr["MAX"] = int.Parse(gvr.Cells[4].Text);
dr["MIN"] = int.Parse(gvr.Cells[5].Text);
dr["AVG"] = int.Parse(gvr.Cells[6].Text);
dr["PERCENTILE25"] = int.Parse(gvr.Cells[7].Text);
dr["PERCENTILE50"] = int.Parse(gvr.Cells[8].Text);
dr["PERCENTILE75"] = int.Parse(gvr.Cells[9].Text);
tg.Rows.Add(dr);
}
}
Chart Chart1= new Chart();
Chart1.DataSource = tg;
Chart1.Width = 600;
Chart1.Height = 350;
Chart1.Series.Add(new Series());
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
List<object> lst = tg.AsEnumerable().ToList<object>();
foreach (DataRow row in tg.Rows)
Chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["MAX"], row["MIN"], row["AVG"], row["PERCENTILE25"], row["PERCENTILE50"], row["PERCENTILE75"] });
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
if (tg.Rows.Count > 0)
{
string title = (tg.Rows[0]["TITLE"].ToString());
Chart1.Titles.Add(title);
}
//create chartareas
ChartArea ca = new ChartArea();
//ca.Name = "ChartArea3";
ca.AxisX = new Axis();
ca.AxisY = new Axis();
Chart1.ChartAreas.Add(ca);
//databind
Chart1.DataBind();
Chart1.Visible = true;
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText += item.Text;
container.Tabs.Add(panel);
panel.Controls.Add(Chart1);
}
}
else
{
foreach (ListItem item in 1stListBox.Items)
{
if (item.Selected)
{
Label tabContent = new Label();
tabContent.ID += item.Value;
tabContent.Text += item.Value;
//string itemSelected = 1stListBox.SelectedItem.ToString();
TextBox1.Text = tabContent.Text.ToString();
DataTable tg = new DataTable();
DataRow dr;
tg.Columns.Add(new DataColumn("DATE"));
tg.Columns.Add(new DataColumn("STATUS", typeof(string)));
tg.Columns.Add(new DataColumn("TITLE", typeof(string)));
tg.Columns.Add(new DataColumn("NAME", typeof(string)));
tg.Columns.Add(new DataColumn("MAX", typeof(int)));
tg.Columns.Add(new DataColumn("MIN", typeof(int)));
tg.Columns.Add(new DataColumn("AVG", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE25", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE50", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE75", typeof(int)));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[2].Text == tabContent.Text.ToString())
{
dr = tg.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["STATUS"] = gvr.Cells[1].Text;
dr["TITLE"] = gvr.Cells[2].Text;
dr["NAME"] = gvr.Cells[3].Text;
dr["MAX"] = int.Parse(gvr.Cells[4].Text);
dr["MIN"] = int.Parse(gvr.Cells[5].Text);
dr["AVG"] = int.Parse(gvr.Cells[6].Text);
dr["PERCENTILE25"] = int.Parse(gvr.Cells[7].Text);
dr["PERCENTILE50"] = int.Parse(gvr.Cells[8].Text);
dr["PERCENTILE75"] = int.Parse(gvr.Cells[9].Text);
tg.Rows.Add(dr);
}
}
Chart chart1= new Chart();
chart1.DataSource = tg;
chart1.Width = 600;
chart1.Height = 350;
chart1.Series.Add(new Series());
chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
List<object> lst = tg.AsEnumerable().ToList<object>();
foreach (DataRow row in tg.Rows)
chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["MAX"], row["MIN"], row["AVG"], row["PERCENTILE25"], row["PERCENTILE50"], row["PERCENTILE75"] });
chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
if (tg.Rows.Count > 0)
{
string title = (tg.Rows[0]["TITLE"].ToString());
chart1.Titles.Add(title);
}
//create chartareas
ChartArea ca = new ChartArea();
//ca.Name = "ChartArea3";
ca.AxisX = new Axis();
ca.AxisY = new Axis();
chart1.ChartAreas.Add(ca);
//databind
chart1.DataBind();
chart1.Visible = true;
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText += item.Value;
container.Tabs.Add(panel);
//panel.Controls.Add(tabContent);
panel.Controls.Add(chart1);
}
}
}
PlaceHolder1.Controls.Add(container);
}
BindDropDownList1() 方法:
public void BindDropDownList1()
{
//Lost to hold the values
List<DateTime> listCopy = new List<DateTime>();
DateTime dt;
string values = String.Join(", ", JOBRUN_CBL.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text));
if (values.Contains("Select All"))
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
if (DateTime.TryParse(li.Text, out dt))
{
listCopy.Add(dt);
}
}
}
else
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
//check if item is selected
if (li.Selected == true)
{
//add items to list
listCopy.Add(DateTime.Parse(li.Text));
}
}
}
//compare and sort so that the latest date comes on top
listCopy.Sort((x, y) => y.CompareTo(x));
//clear the items in dropdownlist
DropDownList1.Items.Clear();
//set the datasource to dropdownlist
DropDownList1.DataSource = listCopy;
//set the dateformatstring in dropdownlist
DropDownList1.DataTextFormatString = "{0:dd-MMM-yyyy}";
//Bind the dropdownlist
DropDownList1.DataBind();
}
当用户单击下拉列表中的不同选项时,我如何获得单击的内容 来自 DropDownList(基于 ListItem Text(例如项目 2),not 值(例如 1),然后从中检索数据gridview根据被点击的内容,根据被点击的内容在网页上显示不同的内容?
您需要 autopostback
和 selectedindexchanged event
才能做到这一点。
根据下面的代码:
如果你想让它在 Page_Load
上触发,你可能想在 Page_Load
上调用事件,或者你将第一个项目绑定到你的 gridview
.
.aspx
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Test 1</asp:ListItem>
<asp:ListItem>Test 2</asp:ListItem>
<asp:ListItem>Test 3</asp:ListItem>
<asp:ListItem>Test 4</asp:ListItem>
</asp:DropDownList>
.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedIndex >= 0)
{
// Get your selected Text
string value = ddl.SelectedItem.Text.Trim();
// Do your binding here
}
}
我有一个下拉列表供用户 select 他们想要从 gridview 检索的项目。 (见下文:)
假设首先加载页面时,下拉列表 "Item1" 显示为默认选项。所以首先当页面加载时,我应该根据 ListItem 文本而不是值检索 Item1 内容,并在网页上显示 Item1 内容。如果用户点击了不同的选项(例如Item2),那么我会根据ListItem Text检索Item2的内容,并在网页上显示Item2的内容,等等..
下拉列表代码:
<asp:Dropdownlist ID="DropDownList1" runat="server" Width="136px" AutoPostBack="true" OnSelectedIndexChanged="jobRun_SelectedIndexChanged">
</asp:Dropdownlist>
DropDownList_SelectedIndexChanged代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex >= 0)
{
if (!string.IsNullOrEmpty(1stListBox.SelectedValue) && !string.IsNullOrEmpty(2ndListBox.SelectedValue) && !string.IsNullOrEmpty(3rdListBox.SelectedValue)
&& !string.IsNullOrEmpty(4thListBox.SelectedValue) && !string.IsNullOrEmpty(5thListBox.SelectedValue))
{
1stListBox_LABEL.Text = string.Empty;
2ndListBox_LABEL.Text = string.Empty;
3rdListBox_LABEL.Text = string.Empty;
4thListBoxLABEL.Text = string.Empty;
5thListBox_LABEL.Text = string.Empty;
BindDropDownList1();
}
else
{
if (string.IsNullOrEmpty(1stListBox.SelectedValue))
{
1stListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
1stListBox_LABEL.Text = string.Empty;
}
if (string.IsNullOrEmpty(1stListBox.SelectedValue))
{
1stListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
1stListBox_LABEL.Text = string.Empty;
}
if (2ndListBox.SelectedValue == String.Empty)
{
2ndListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
2ndListBox_LABEL.Text = string.Empty;
}
if (3rdListBox.SelectedValue == String.Empty)
{
3rdListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
3rdListBox_LABEL.Text = string.Empty;
}
if (4thListBox.SelectedValue == String.Empty)
{
4thListBox_LABEL.Text = ("Please select at least one option").ToString();
}
else
{
4thListBox.Text = string.Empty;
}
}
AjaxControlToolkit.TabContainer container = new AjaxControlToolkit.TabContainer();
container.ID = "TabContainer";
container.EnableViewState = false;
container.Tabs.Clear();
container.Height = Unit.Pixel(2000);
container.Width = Unit.Pixel(1200);
container.Tabs.AddAt(0, GetManualTab());
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ID = "HEATMAP";
img.Width = 40;
img.Height = 300;
img.ImageUrl = "Image/HEATMAP.png";
string selectedValue= DropDownList1.SelectedItem.ToString();
if (1stListBox.SelectedValue == "Select All")
{
foreach (ListItem item in 1stListBox.Items)
{
if (item.Value.Equals("1"))
{
continue;
}
Label tabContent = new Label();
tabContent.ID += item.Value;
tabContent.Text += item.Value;
string itemSelected = 1stListBox.SelectedItem.ToString();
TextBox1.Text = tabContent.Text.ToString();
DataTable tg = new DataTable();
DataRow dr;
tg.Columns.Add(new DataColumn("DATE"));
tg.Columns.Add(new DataColumn("STATUS", typeof(string)));
tg.Columns.Add(new DataColumn("TITLE", typeof(string)));
tg.Columns.Add(new DataColumn("NAME", typeof(string)));
tg.Columns.Add(new DataColumn("MAX", typeof(int)));
tg.Columns.Add(new DataColumn("MIN", typeof(int)));
tg.Columns.Add(new DataColumn("AVG", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE25", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE50", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE75", typeof(int)));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[2].Text == tabContent.Text.ToString() && gvr.Cells[0].Text == selectedValue.ToString())
{
dr = tg.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["STATUS"] = gvr.Cells[1].Text;
dr["TITLE"] = gvr.Cells[2].Text;
dr["NAME"] = gvr.Cells[3].Text;
dr["MAX"] = int.Parse(gvr.Cells[4].Text);
dr["MIN"] = int.Parse(gvr.Cells[5].Text);
dr["AVG"] = int.Parse(gvr.Cells[6].Text);
dr["PERCENTILE25"] = int.Parse(gvr.Cells[7].Text);
dr["PERCENTILE50"] = int.Parse(gvr.Cells[8].Text);
dr["PERCENTILE75"] = int.Parse(gvr.Cells[9].Text);
tg.Rows.Add(dr);
}
}
Chart Chart1= new Chart();
Chart1.DataSource = tg;
Chart1.Width = 600;
Chart1.Height = 350;
Chart1.Series.Add(new Series());
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
List<object> lst = tg.AsEnumerable().ToList<object>();
foreach (DataRow row in tg.Rows)
Chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["MAX"], row["MIN"], row["AVG"], row["PERCENTILE25"], row["PERCENTILE50"], row["PERCENTILE75"] });
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
if (tg.Rows.Count > 0)
{
string title = (tg.Rows[0]["TITLE"].ToString());
Chart1.Titles.Add(title);
}
//create chartareas
ChartArea ca = new ChartArea();
//ca.Name = "ChartArea3";
ca.AxisX = new Axis();
ca.AxisY = new Axis();
Chart1.ChartAreas.Add(ca);
//databind
Chart1.DataBind();
Chart1.Visible = true;
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText += item.Text;
container.Tabs.Add(panel);
panel.Controls.Add(Chart1);
}
}
else
{
foreach (ListItem item in 1stListBox.Items)
{
if (item.Selected)
{
Label tabContent = new Label();
tabContent.ID += item.Value;
tabContent.Text += item.Value;
//string itemSelected = 1stListBox.SelectedItem.ToString();
TextBox1.Text = tabContent.Text.ToString();
DataTable tg = new DataTable();
DataRow dr;
tg.Columns.Add(new DataColumn("DATE"));
tg.Columns.Add(new DataColumn("STATUS", typeof(string)));
tg.Columns.Add(new DataColumn("TITLE", typeof(string)));
tg.Columns.Add(new DataColumn("NAME", typeof(string)));
tg.Columns.Add(new DataColumn("MAX", typeof(int)));
tg.Columns.Add(new DataColumn("MIN", typeof(int)));
tg.Columns.Add(new DataColumn("AVG", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE25", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE50", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE75", typeof(int)));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[2].Text == tabContent.Text.ToString())
{
dr = tg.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["STATUS"] = gvr.Cells[1].Text;
dr["TITLE"] = gvr.Cells[2].Text;
dr["NAME"] = gvr.Cells[3].Text;
dr["MAX"] = int.Parse(gvr.Cells[4].Text);
dr["MIN"] = int.Parse(gvr.Cells[5].Text);
dr["AVG"] = int.Parse(gvr.Cells[6].Text);
dr["PERCENTILE25"] = int.Parse(gvr.Cells[7].Text);
dr["PERCENTILE50"] = int.Parse(gvr.Cells[8].Text);
dr["PERCENTILE75"] = int.Parse(gvr.Cells[9].Text);
tg.Rows.Add(dr);
}
}
Chart chart1= new Chart();
chart1.DataSource = tg;
chart1.Width = 600;
chart1.Height = 350;
chart1.Series.Add(new Series());
chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
List<object> lst = tg.AsEnumerable().ToList<object>();
foreach (DataRow row in tg.Rows)
chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["MAX"], row["MIN"], row["AVG"], row["PERCENTILE25"], row["PERCENTILE50"], row["PERCENTILE75"] });
chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
if (tg.Rows.Count > 0)
{
string title = (tg.Rows[0]["TITLE"].ToString());
chart1.Titles.Add(title);
}
//create chartareas
ChartArea ca = new ChartArea();
//ca.Name = "ChartArea3";
ca.AxisX = new Axis();
ca.AxisY = new Axis();
chart1.ChartAreas.Add(ca);
//databind
chart1.DataBind();
chart1.Visible = true;
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText += item.Value;
container.Tabs.Add(panel);
//panel.Controls.Add(tabContent);
panel.Controls.Add(chart1);
}
}
}
PlaceHolder1.Controls.Add(container);
}
BindDropDownList1() 方法:
public void BindDropDownList1()
{
//Lost to hold the values
List<DateTime> listCopy = new List<DateTime>();
DateTime dt;
string values = String.Join(", ", JOBRUN_CBL.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text));
if (values.Contains("Select All"))
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
if (DateTime.TryParse(li.Text, out dt))
{
listCopy.Add(dt);
}
}
}
else
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
//check if item is selected
if (li.Selected == true)
{
//add items to list
listCopy.Add(DateTime.Parse(li.Text));
}
}
}
//compare and sort so that the latest date comes on top
listCopy.Sort((x, y) => y.CompareTo(x));
//clear the items in dropdownlist
DropDownList1.Items.Clear();
//set the datasource to dropdownlist
DropDownList1.DataSource = listCopy;
//set the dateformatstring in dropdownlist
DropDownList1.DataTextFormatString = "{0:dd-MMM-yyyy}";
//Bind the dropdownlist
DropDownList1.DataBind();
}
当用户单击下拉列表中的不同选项时,我如何获得单击的内容 来自 DropDownList(基于 ListItem Text(例如项目 2),not 值(例如 1),然后从中检索数据gridview根据被点击的内容,根据被点击的内容在网页上显示不同的内容?
您需要 autopostback
和 selectedindexchanged event
才能做到这一点。
根据下面的代码:
如果你想让它在 Page_Load
上触发,你可能想在 Page_Load
上调用事件,或者你将第一个项目绑定到你的 gridview
.
.aspx
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Test 1</asp:ListItem>
<asp:ListItem>Test 2</asp:ListItem>
<asp:ListItem>Test 3</asp:ListItem>
<asp:ListItem>Test 4</asp:ListItem>
</asp:DropDownList>
.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedIndex >= 0)
{
// Get your selected Text
string value = ddl.SelectedItem.Text.Trim();
// Do your binding here
}
}