Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I am having issue with asp: DropDownList . After the 'Retrieve' button is clicked, by right my string 'DropdownValueSelected' should store the option selected from the DropDownList. The problem is that the DropDownList returns the first item every time no matter which option is selected. I have researched online and tried various ways but still couldn't solve this.

This is my codes:

Aspx file:
ASP.NET
 <asp:Dropdownlist ID="DropDownListSelection" runat="server" Width="136px" AutoPostBack ="True" AppendDataBoundItems="true" EnableViewState="true" ViewStateMode="Enabled"> 
</asp:Dropdownlist>

     <asp:Button ID="RETRIEVE_BUTTON" runat="server" Height="37px" Text="Retrieve" Width="131px" OnClick="RETRIEVE_BUTTON_Click" style="font-weight:bold" BackColor="#333333" BorderColor="White" BorderStyle="Groove" ForeColor="White" ViewStateMode="Inherit" />
<br />

Cs file:
HTML
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindChart();
            }
        )

protected void RETRIEVE_BUTTON_Click(object sender, EventArgs e)
        {
           BindChart();
        }

public void BindChart()
        {
            string selectedValue = DropDownListSelection.SelectedItem.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("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[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["MAX"] = gvr.Cells[3].Text;
                    dr["MIN"] = gvr.Cells[4].Text;
                    dr["AVG"] = gvr.Cells[5].Text;
                    dr["PERCENTILE25"] = gvr.Cells[6].Text;
                    dr["PERCENTILE50"] = gvr.Cells[7].Text;
                    dr["PERCENTILE75"] = gvr.Cells[8].Text;
                    tg.Rows.Add(dr);

                }
            }

Basically, based on what user has selected, I want to store value selected in a string called 'selectedValue'. Then i will find 'selectedValue' from one column in a gridview. If any row has the 'selectedValue' from that particular column in the gridview, i will add those rows into a datatable. But everytime DropDownList returns me the first item and only store first item into the string no matter which option from the DropDownList is selected.

Question: How to store what user has selected from a DropDownList into a string and not let DropDownList returns the first item every time no matter which option is selected from DropDown List.

I need help on this. Greatly appreciate if someone can help me on this, thanks!
Posted
Comments
j snooze 28-Sep-15 17:12pm    
I do not see anywhere listed that you are filling the drop down list. If that is what you are having a problem with, I think that may be relevant to the issue. Please post the code where you are filling the drop down.
Mathi Mani 28-Sep-15 17:15pm    
Can you add the code show where you are binding data to the DropDownList?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900