简体   繁体   中英

How to get asp Dropdownlist selected value in c# file?

I have a follow dropdown list in my aspx page:

 <asp:DropDownList ID="OrderPeriodStatus" runat="server" AutoPostBack="true"
    CssClass="ddlb" Width="100px" Height="30px" DataSourceID="SqlDataSource5" 
    DataTextField="OrderPeriod" DataValueField="OrderPeriodID" onselectedindexchanged="OrderPeriodStatus_SelectedIndexChanged">
     </asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
    </asp:SqlDataSource>

I tried to access the value of selected value in C# by following code:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ProcessEligibleScenarios();
            LoadOptions();
            ddlbPeriod.DataBind();
            GridView1.DataBind();
            gvActiveLogs.DataBind();
        }
    }

protected void OrderPeriodStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (OrderPeriodStatus.SelectedValue != null)
        {
            SqlConnection myConn = default(SqlConnection);
            SqlCommand myComm = default(SqlCommand);
            myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProdDB"].ConnectionString);
            myConn.Open();
            myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
            try
            {
                myComm.Connection = myConn;
                SqlDataReader Dr = myComm.ExecuteReader();
                while (Dr.Read())
                {
                    System.Diagnostics.Debug.Write("While reading the data");
                    TextBox1.Text = Dr["OrderPeriodDate"].ToString();
                    TextBox2.Text = Dr["OrderPeriodStatus"].ToString();
                    NotesArea.Value = (string)Dr["Notes"];
                    System.Diagnostics.Debug.WriteLine(OrderPeriodStatus.SelectedValue);
                }
                Dr.Close();
                myConn.Close();
            }
            catch (SqlException sqx)
            {
            }
            GridView1.DataBind();
        }
        else {  }
    }

But when i print the value . It shows the selected Index not the value. Why?

You have mismatch error on sqdatasource, replace with SqlDataSource4

DataSourceID="SqlDataSource5"  <---

<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
 </asp:SqlDataSource>

If you set AutoPostback property on the dropdownlist to true, and ViewStateMode to inherit, you should get value you selected. I tried it on my side, and it worked. Since you already have SqlDataSource, and the DataSourceId of the dropdownlist is set to the SqlDataSource, it should be populated automatically.

---edit

Sorry, I think I might have misread your question. If you instead use

DropDownListnew.SelectedItem.Text

you should get the text value you want.

-- another edit:

@Amit, you will get index from SelectedValue, because in your dropdownlist code, you have set

DataValueField="OrderPeriodID"

which is basically your primary key or index. To get the text value instead of index you have to use

SelectedItem.Text

Also in your query:

myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");

I think you should be using

WHERE OrderPeriodID ='" + OrderPeriodStatus.SelectedValue + "'");

You are missing the ID bit after OrderPeriod.

You can try get the value from Request.Form object and to get the text use ddl.Items.FindByValue method

    var value = Request.Form[DropDownListnew.UniqeID];
    var text = DropDownListnew.Items.FindByValue(value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM