سؤال

I'm using an html select tag to have a list of values from my data base (column name is "DB"), in the page load - the list is populated with the right values but after the user selects a value from the list and clicks a button, when I'm trying to retrieve this value - I always get the value of the first item in the list regardless of user's selection.

aspx code:

<select id="id1" runat="server" AutoPostBack="True"></select>
<asp:Button ID="Submit" runat="server" onclick="Button1_Click" />

c# code:

protected void Page_Load(object sender, EventArgs e)
{
    InstanceData = (DataSet)(Session["InstanceData"]);
    id1.DataSource = InstanceData.tables[0];
    id1.DataTextField = "DB";
    id1.DataValueField = "DB";
    id1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    string DataBase = id1.Value;
}
هل كانت مفيدة؟

المحلول

Below code will help you

 protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        {
         InstanceData = (DataSet)(Session["InstanceData"]);
         id1.DataSource = InstanceData.tables[0];
         id1.DataTextField = "DB";
         id1.DataValueField = "DB";
         id1.DataBind();
        }
    }

 protected void Button1_Click(object sender, EventArgs e)
    {
        string DataBase = id1.SelectedValue;
    }

نصائح أخرى

Take the value on the SelectedIndexChanged event if you are using dropdown list.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top