Reputation: 57
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();
Upvotes: 1
Views: 2225
Reputation: 26
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
Upvotes: 0
Reputation: 2294
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.
Upvotes: 1
Reputation: 57
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
}
Upvotes: 0
Reputation: 3834
While binding datasource to combo, mention DataMember
and DataValue
property for combo box so that selecting elements from combo you get selectedValue
as DataValue
.
Upvotes: 0