简体   繁体   中英

How to save the drop down list selected index in asp.net?

i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.

The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.

I have done some research, and so far i cannot solve it, hope someone can help me out.

Code for Dropdown list:-

<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList> 

here's how i fill the drop-down-list

ClsUtil clsutil = new ClsUtil(); 
DataSet ds = clsutilerias.FillDDL(Category); 
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString(); 
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0]; 
ddlCategory.DataBind(); 

The DropDownList has properties. You need to set SelectedIndex , SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).

Edit : Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndex perhaps as much as get the SelectedIndex . Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.

Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)

It seems that you found your solution in checking IsPostback in the Page , and not re-populate the list if the value is true . That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.

Another option, of course, would be to read the posted data in any event that happens before you do the data-binding . You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again , if you don't like having ASP.NET doing it for you.

在将数据源绑定到组合时,请为组合框提及DataMemberDataValue属性,以便从组合中选择元素,您将获得selectedValue作为DataValue

Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:

if(!IsPostBack)
{
 //here goes the code to fill the Drop down list
}

When you are clicking the button, the entire page is refreshing that why you got the first index all the time The best solution is :

if(IsPostBack == false)
{
//Save your data code here
}

You have other tricky solution which is : put the dropboxmenu inside an update panel control

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