drop down list problem
-
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
-
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
Are you filling the DropDownList with data when the page loads? i.e. in this method:
private void Page_Load(object sender, System.EventArgs e) { }
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook -
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
-
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
Use the following code to fill combo dim dread As SqlDataReader Conn.Open() SqlCommand1.CommandText = "Select * from Table" dread = SqlCommand1.ExecuteReader ddl.DataSource = dread ddl.DataTextField = "Field1" ddl.DataValueField = "Field2" ddl.DataBind() dread.Close() On the Selectedindexchange of dropdown use this code label1.text=ddl.SelectedItem.text ' To get the item seleted label1.text=ddl.SelectedItem.Value ' To get value of item selected. hope this will help you.
-
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
I think u r filling Up the data in Dropdown at page load.If yes,Then at page load put page load() If not ispostback then //put the code to fill the dropdown here end if So when u submit u will see it moves to page load and then to submit function and put auto post back property of dropDown=True Bye Varun
-
I think u r filling Up the data in Dropdown at page load.If yes,Then at page load put page load() If not ispostback then //put the code to fill the dropdown here end if So when u submit u will see it moves to page load and then to submit function and put auto post back property of dropDown=True Bye Varun
-
Are you filling the DropDownList with data when the page loads? i.e. in this method:
private void Page_Load(object sender, System.EventArgs e) { }
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook -
Are you filling the DropDownList with data when the page loads? i.e. in this method:
private void Page_Load(object sender, System.EventArgs e) { }
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook -
Use the following code to fill combo dim dread As SqlDataReader Conn.Open() SqlCommand1.CommandText = "Select * from Table" dread = SqlCommand1.ExecuteReader ddl.DataSource = dread ddl.DataTextField = "Field1" ddl.DataValueField = "Field2" ddl.DataBind() dread.Close() On the Selectedindexchange of dropdown use this code label1.text=ddl.SelectedItem.text ' To get the item seleted label1.text=ddl.SelectedItem.Value ' To get value of item selected. hope this will help you.
-
OK, the behavior you are getting is normal in this case. To understand why it is causing a problem, you need to understand how the execution of the page is done. Suppose you have a page that has been loaded and your dropdownlist is filled with data from the database. Now suppose the user selects something from the dropdownlist. When the page is posted back (either through autopostback or through a click of a button) the selectedIndexChanged event method will not be executed immediately..First, ASP.NET will execute the Page_Load method, and THEN it will execute the SelectedIndexChanged method. This means that if you are filling your dropdownlist in the Page_Load, it will be filled at every postback and consequently, the index 0 will always be selected (since this is what happens when a dropdownlist is filled) To remedy this problem, you need to make the fill of the dropdownlist only on the first load. After that, the data will remain in the dropdownlist if you keep the ViewState to true, which is by default the case. You need to use the isPostBack flag to determine if the page is being posted back or if it is the first load of the page. You would write something like this in the Page_Load method:
// ...some code if (!IsPostBack) // on first load only { // here fill the dropdownlist from data from database } // ...some more code
Doing this will resolve your problem (I hope!) Talal "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook -
OK, the behavior you are getting is normal in this case. To understand why it is causing a problem, you need to understand how the execution of the page is done. Suppose you have a page that has been loaded and your dropdownlist is filled with data from the database. Now suppose the user selects something from the dropdownlist. When the page is posted back (either through autopostback or through a click of a button) the selectedIndexChanged event method will not be executed immediately..First, ASP.NET will execute the Page_Load method, and THEN it will execute the SelectedIndexChanged method. This means that if you are filling your dropdownlist in the Page_Load, it will be filled at every postback and consequently, the index 0 will always be selected (since this is what happens when a dropdownlist is filled) To remedy this problem, you need to make the fill of the dropdownlist only on the first load. After that, the data will remain in the dropdownlist if you keep the ViewState to true, which is by default the case. You need to use the isPostBack flag to determine if the page is being posted back or if it is the first load of the page. You would write something like this in the Page_Load method:
// ...some code if (!IsPostBack) // on first load only { // here fill the dropdownlist from data from database } // ...some more code
Doing this will resolve your problem (I hope!) Talal "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook -
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
you can try getting the value by Request["dropdownName"].ToString(); Subhasis Chandra -- modified at 8:14 Wednesday 31st May, 2006
-
hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif
Try, strtext= ddlist.SelectedValue Let me know if you still having problem. ======= Examples: http://www.programmingknowledge.com/[^]