Hi
I'm using a WebDropDown with an ItemTemplate that renders multiple columns from an underlying datasource, just like shown in the video from craigshoemaker. I've set the AutoPostback property to true. Everything loads well when I run the page. When I select a value the page does a postback (as desired).But now all the items from the WebDropDown are empty, however the template-layout is still applied and the Value and Text properties are still filled up.It seems that the DataItems get null, because they're not being saved in the ViewState?
Is this normal behavior? The only workaround I can find is to DataBind the control on every postback.
I've attached a sample project (VB.NET and version 9.1.20091.2040)
Hi Angel
On one hand I can understand that's not being saved in the ViewState (it would get bloated), on the other hand if you would use multiple WebDropDown-controls that binds on every postback (for instance if they were cascaded), you would also get a performance issue, not?
Anyway thanks for your help.
Kind regards
Kevin
Hi Kevin,
If you disable ViewState (EnableViewState=false), and do DataBind on every postback, I think there shouldn't be any noticeable performance hit.
Please let me know if you notice any degradation there.
Thanks,
Angel
I have the same issue and I have done what is suggested above but I still have empty items when I select the 2nd time. Here is the code. Why wont the webdropdown display the items the 2nd time? If I remove the ItemTemplate all is well except that the items are shown as hyperlinks which I do not want.
<ig:WebDropDown ID="webDropDownProvince0" runat="server" DisplayMode="DropDownList" Width="200px" TabIndex="4" AutoPostBack="True" ondatabound="webDropDownProvince0_DataBound" onselectionchanged="webDropDownProvince0_SelectionChanged" EnableViewState="False"> <DropDownItemBinding TextField="ProvinceName" ValueField="ProvinceName" /> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "ProvinceName")%> </ItemTemplate> </ig:WebDropDown>
Ok now it's clear to me. DataItem for controls is not kept in ViewState, only the item is - that is, the actual DropDownItem. Therefore if you do templating and bind in this way, referring to the DataItem, this is the expected behavior.
To fix this, you can call DataBind() on every postback , and it will work fine.
By the way, you don't need to use ItemTemplate if you only want to show text , you can bind the Text property and it will automatically render the item's text.
I do call DataBind on each PostBack and I still have the same result.
What do you mean by bind the Text property?
if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add("ProvinceName"); dt.Rows.Add("British Columbia"); dt.Rows.Add("Alberta"); dt.Rows.Add("Saskatchewan"); dt.Rows.Add("Manitoba"); dt.Rows.Add("Ontario"); dt.Rows.Add("Quebec"); dt.Rows.Add("New Brunswick"); dt.Rows.Add("Nova Scotia"); dt.Rows.Add("Newfoundland/Labrador"); dt.Rows.Add("Prince Edward Island"); dt.Rows.Add("Nunavut"); dt.Rows.Add("Yukon"); dt.Rows.Add("Northwest Territories");
webDropDownProvince0.DataSource = dt; webDropDownProvince0.ValueField = "ProvinceName"; webDropDownProvince0.TextField = "ProvinceName"; } webDropDownProvince0.DataBind();
Dont worry about the previous question. Speed is not an issue. It was because I was running in the debugger. When I run it on a web server in Release mode it is plenty fast.
Ok, that makes sense. On one of my controls which is bound to a SqlDataSource object, ti takes a while to load since the list is large at 317 items. Do you have any suggestions for that?
Hi David,
This is only when you use templating and access the DataItem without rebinding the control. We do not store the DataItem in ViewState - this will make it enourmous.
Only the WebDataGrid enables this behavior by setting the EnableDataViewState property to true.
In your example you have to save a reference to the data source because you are binding to a DataSource object. By doing so, after you rebind the control, you get back all the DataItems.
Ok, that all works, but why do I have to save the datasource with the WebDropDown and I dont have to do it with the WebCombo or the regular .NET combobox? That sure sounds like a bug with the control.
About DataBinding, it doesn't work in your case and you see empty items, because you are not storing the datasource in the session, and then restoring it back:
webDropDownProvince.DataSource = dt;
webDropDownProvince.ValueField = "ProvinceName";
webDropDownProvince.TextField = "ProvinceName";
Session["DropDownDataSource"] = dt;
}
webDropDownProvince.DataSource = Session["DropDownDataSource"];
webDropDownProvince.DataBind();
protected void webDropDownProvince_DataBound(object sender, EventArgs e)
{
// webDropDownProvince.SelectedItemIndex = 0;
// webDropDownProvince_SelectionChanged(null, null);
I commented the code to reset selection in the DataBound method, because it will wipe out the selection once the dropdown is rebound every time.
In my previous post I have removed the <ItemTemplate> markup ,and i don't see the underlined links (screenshot).
Hope it helps. Please let me know if i can assist with anything else.
Thank you,