So I found a tricky problem with databound DropDownLists that causes the selectedIndexChanged event to fire more than you probably intended.
Normally when you add one - you might DataBind it, and then do something like
my_ddl.Items.Add(new ListItem("Select . . . "));
This can become tricky pretty fast if your DropDownList does anything major - like add a selection to a form. The problem is that dropdownlists seem to be loaded with a SelectedIndex of -1. This means that when they are unbound (aka have no data at all), they don't want to put 0 (because that would indicate an item) - so its a -1.
So you bind your data, and then add a top level ListItem for convenience - and now the selected Index is 0 - so SelectedIndexChanged event fires!
The simple fix is to force all of your DDL's to initialize to 0 - and always have a top item! That way, if a user selects the top item, the event does not fire, but any other databound item will work fine.
So, in Page_load, do this
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
my_ddl.SelectedIndex = 0;
}
Note that this only needs to be done for databound controls / controls created programmatically. I have not extensively tested, so I am not 100% on what controls this affects, but I am pretty sure that ones created declaratively in the ASPX page will not have this problem
Hamy
Showing posts with label dropdownlist. Show all posts
Showing posts with label dropdownlist. Show all posts
Thursday, July 24, 2008
Managing DataBound DropDownLists Properly
Posted by
Hamilton
at
9:32 AM
Labels:
asp.net,
dropdownlist,
managing,
tricky errors
Monday, July 21, 2008
When Databinding results in System.Byte[]
Posted by
Hamilton
at
9:01 PM
So - quite a few people seem to be having an issue with DataBinding to any sort of control (Dropdownlist, DataGrid, etc) resulting in the bound control printing out System.Byte[]. This may not be the one true answer, but it is definitely one of them.
I noticed a lot of people (like myself) were having trouble with MySql - specifically connector/net. The solution is simple, and probably affects other database storage engines just as well.
Basically - the just of it was this - here is my code to bind a DropDownList. Pretty readable stuff
The issue with this case was that my procedure calculated the result of "extended_varname" - which means that it was created by doing a SELECT CONCAT(name,".",id) sort of deal. Which meant there was no column type associated with it. The solution - pretty simple. Just cast the result. I did this - SELECT CAST(CONCAT(name,".",id) AS CHAR) AS extended_varname . While I believe that you might be able to so that simpler, it was the easy answer. If I figure out a better way to cast it, I will let you know - but the point is the general idea that the column needs to have a specific type associated with it before MySql Connector can correctly convert the rows to strings - any System.Byte[] result generally indicates a cast went wrong somewhere.
Hope that helps
-Hamy
References
mysql type conversion
I noticed a lot of people (like myself) were having trouble with MySql - specifically connector/net. The solution is simple, and probably affects other database storage engines just as well.
Basically - the just of it was this - here is my code to bind a DropDownList. Pretty readable stuff
----------------------------
string sql = "CALL Get_LFID_info('" + ddl_Log_Formats_1.SelectedValue + "');";
DataTable dts = ExecuteMySqlAdapter(sql);
relation1.DataSource = dts;
relation1.DataTextField = "extended_varname";
relation1.DataValueField = "variable_id";
relation1.DataBind();
----------------------------
The issue with this case was that my procedure calculated the result of "extended_varname" - which means that it was created by doing a SELECT CONCAT(name,".",id) sort of deal. Which meant there was no column type associated with it. The solution - pretty simple. Just cast the result. I did this - SELECT CAST(CONCAT(name,".",id) AS CHAR) AS extended_varname . While I believe that you might be able to so that simpler, it was the easy answer. If I figure out a better way to cast it, I will let you know - but the point is the general idea that the column needs to have a specific type associated with it before MySql Connector can correctly convert the rows to strings - any System.Byte[] result generally indicates a cast went wrong somewhere.
Hope that helps
-Hamy
References
mysql type conversion
Labels:
.net,
asp,
asp.net,
bind,
data,
databinding,
dropdownlist,
errors,
System.Byte[]
Subscribe to:
Posts (Atom)