如何在用户从DropDownList中选择不同选项时更改内容

时间:2015-09-30 00:56:53

标签: javascript c# asp.net dropdown

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

enter image description here

让我们首先说明页面加载时,下拉列表是" Item1"显示为默认选项。因此,首先加载页面时,我应该根据ListItem Text检索Item1内容,而不是值,并在网页上显示Item1内容。如果用户点击了其他选项(例如Item2),那么我将基于ListItem文本检索Item2内容,并在网页上显示Item2内容,依此类推..

DropDownList代码:

 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);
        }

DropDownList_SelectedIndexChanged代码:

       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();
    }

BindDropDownList1()方法:

import static java.lang.System.*;

public class StringEquality
{
   private String wordOne, wordTwo;

   public StringEquality()
   {
   }

   public StringEquality(String one, String two)
   {
      setWords (wordOne, wordTwo);
   }

   public void setWords(String one, String two)
   {
      wordOne = one;
      wordTwo = two;
   }

   public boolean checkEquality()
   {
      if (wordOne == wordTwo)
      return true;
      else
      return false;
   }

  public String toString()
  {
    String output = "";
    if (checkEquality())
    output += wordOne + " does not have the same letters as " + wordTwo;
    else
    output += wordOne + " does have the same letters as " + wordTwo;
    return output;
  }
}

问题:当用户点击从下拉列表中不同选项时,如何从DropDownList中获取点击的内容(基于ListItem 文本(例如项目2),值(例如1),然后根据被点击的内容从gridview检索数据,并显示不同根据点击内容在网页上显示内容?

尽管在线搜索了很长时间,但我不知道如何做到这一点。  如果有人能为我提供帮助,我将不胜感激。谢谢!

此致

费利西亚

1 个答案:

答案 0 :(得分:2)

我可能会错误地理解你的问题,但它是C#问题还是javascript问题
您需要autopostbackselectedindexchanged 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


    }
}