J
Jim Bancroft
Hi everyone,
I have a DropDownList I populate as outlined below. This is from my
code-behind file:
private void Page_Load(object sender, System.EventArgs e)
{
BindMyData();
DataBind();
}
private void BindMyData()
{
DropDownList2.DataSource = ADONetDataSet.Tables[0];
DropDownList2.DataValueField="ValueField";
DropDownList2.DataTextField="TextField";
DropDownList2.SelectedIndex = 12;
DropDownList2.DataBind();
}
Funny thing: my DropDownList's "SelectedIndex" value won't take hold if I
call the page level DataBind() method above. If I comment out the method,
the SelectedIndex value is set properly.
I've seen other strange behavior when mixing page-level and control-level
DataBind() calls; for example, if I loop through a DataGrid containing
DropDownLists and bind each DropDownList individually, they aren't populated
if I end with a page-level call to DataBind(). As in the previous example,
if I cut that line out they work as advertised.
I'm no expert on when to call DataBind() and under what circumstances I
should call the control vs. page level method. Can anyone provide tips or
best practices regarding this, along with theories as to what's happening in
my code snippet? Thanks very much.
I have a DropDownList I populate as outlined below. This is from my
code-behind file:
private void Page_Load(object sender, System.EventArgs e)
{
BindMyData();
DataBind();
}
private void BindMyData()
{
DropDownList2.DataSource = ADONetDataSet.Tables[0];
DropDownList2.DataValueField="ValueField";
DropDownList2.DataTextField="TextField";
DropDownList2.SelectedIndex = 12;
DropDownList2.DataBind();
}
Funny thing: my DropDownList's "SelectedIndex" value won't take hold if I
call the page level DataBind() method above. If I comment out the method,
the SelectedIndex value is set properly.
I've seen other strange behavior when mixing page-level and control-level
DataBind() calls; for example, if I loop through a DataGrid containing
DropDownLists and bind each DropDownList individually, they aren't populated
if I end with a page-level call to DataBind(). As in the previous example,
if I cut that line out they work as advertised.
I'm no expert on when to call DataBind() and under what circumstances I
should call the control vs. page level method. Can anyone provide tips or
best practices regarding this, along with theories as to what's happening in
my code snippet? Thanks very much.