Last post Aug 19, 2019 08:16 PM by katman
Aug 12, 2019 05:32 PM|katman|LINK
I am having an issue where I am creating a form and on page load (when it isn't posting back), I fill two drop down lists with data from a table in my database, one for departments and the other for organizations.
I fill out the form and select a value from the first drop down list. When I do that, I get the postback flash, and it resets the value for the drop down list back to the default value that was originally set on the first page load. The next time I select
it, it works without issue.
Is there a setting I could be missing somewhere that is causing this value to not be detected/set correctly the first time around?
If you need some portions of code to help determine, I can provide some of that but it seems like I might be missing a property/setting that interferes with the form posting back correctly?
Aug 12, 2019 06:42 PM|katman|LINK
A bit more info with some things I was testing...
In the page load event, I set the value of the dropdown list to index 3. On the first page load, this worked correctly. The dropdown list correctly selects the value that I want at index 3 and sets the selectedindex to 3.
However, if i select any other value (the first time I am selecting only), it flips back to index 3. It is like it isn't detecting that value change or maintaining state on that first selection change.
Any time after that, it works. Scratching my head....
Aug 13, 2019 03:17 AM|Yongqing Yu|LINK
According to your description, What do you mean by
creating form?Do you want to create form tags in code behind when page load?
I hope you can provide me with detailed code for reference, so that we can solve your issue more easily.
I have made an example about your overall demand, you can refer to the following code:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
DropDownList1.DataSource = BindDropdown();
DropDownList1.DataTextField = "FirstName";
DropDownList1.DataValueField = "EmplId";
DropDownList1.SelectedIndex = 3;
DropDownList2.DataSource = BindDropdown();
DropDownList2.DataTextField = "LastName";
DropDownList2.DataValueField = "EmplId";
DropDownList2.SelectedIndex = 3;
public DataTable BindDropdown()
string connstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connstring))
string sql = "select * from Employee";
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
The result of this work demo:
Aug 14, 2019 12:41 PM|katman|LINK
Sorry for the late response. This is not the primary function of my job and I am doing this as I have time. No, this did not resolve my issue. I am essentially already doing what your code outlined.
I have noticed something though. I have a completely different page where I do essentially the same thing and that one is working. The only difference that I could see from an initial glance at my code was that on the one that wasn't working I added the
dropdown list by dragging and dropping it onto my form and then tried to access its methods and properties from the page design interface. However, even on that page, I did fill the dropdown list from code in the Page Load event.
The one that is working, I created the dropdown list entirely with code, not by dragging and dropping it, then using the same code as the other one, I filled it with data from the database. I wouldn't think that would make a difference but I don't know
if when I dragged and dropped it, it set certain properties that I didn't set when I created it in code only. Or if there is something that wasn't set until the first time the form was posted back? Not sure. I will have to look at this some more as I get
time in the next day or two.
Aug 19, 2019 08:16 PM|katman|LINK
So I added in the dropdown list using code instead of dragging and dropping it from the Visual Studio Toolbox and it works as expected. Once again, I am not sure why, whether it is a property that is set or something is not being initialized on the control
However, it is working now and since it was only 2 more lines of code to do it this way, I'm going with this..