当用户从DropDownList选择其他选项时如何更改内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当用户从DropDownList选择其他选项时如何更改内容相关的知识,希望对你有一定的参考价值。

我有一个下拉列表供用户选择要从gridview检索的项目。 (见下文:)

enter image description here

首先说一下,当页面加载时,下拉列表显示为“ Item1”作为默认选项。因此,首先在加载页面时,我应该基于ListItem Text(而不是值)检索Item1内容,并在网页上显示Item1内容。如果用户单击其他选项(例如Item2),那么我将基于ListItem Text检索Item2内容,并在网页上显示Item2内容,依此类推。

DropDownList代码:

            <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检索数据,并根据所单击的内容在网页上显示different内容。

我有一个下拉列表供用户选择要从gridview检索的项目。 (请参见下文:)首先,假设页面加载时,下拉列表显示为“ Item1”,显示为...

答案

您需要具有autopostbackselectedindexchanged event才能这样做。

以上是关于当用户从DropDownList选择其他选项时如何更改内容的主要内容,如果未能解决你的问题,请参考以下文章

C#Combobox“默认文本”DropDownList

Repeater中 DropDownList如何绑定数据

第二个Dropdownlist无法选择其他选项

当用户从下拉菜单中选择选项时,如何从数据库中打印记录?动态程序

当用户从 MS Access 的组合框中选择“其他”时,如何显示输入表单?

Angular 2 如何禁用 kendo-dropdownlist