Language: C#
ASP.NET Listbox rebinding that fails due to SelectedValue set
private void GetResourceSet() { this.ResourceSet = Request.Form[this.lstResourceSet.UniqueID]; if (ResourceSet == null) this.ResourceSet = Request.QueryString["ResourceSet"]; if (this.ResourceSet == null) this.ResourceSet = ViewState["ResourceSet"] as string; if (this.ResourceSet == null) this.ResourceSet = ""; this.ResourceSet = this.ResourceSet.ToLower(); if (!string.IsNullOrEmpty(this.ResourceSet)) ViewState["ResourceSet"] = this.ResourceSet; // *** Clear selections //lstResourceIds.Items.Clear(); lstResourceIds.ClearSelection(); lstResourceSet.Items.Clear(); lstResourceSet.ClearSelection(); DataTable dt = Manager.GetAllResourceSets(ResourceListingTypes.AllResources); this.lstResourceSet.DataSource = dt; this.lstResourceSet.DataValueField = "ResourceSet"; try { this.lstResourceSet.DataBind(); } catch { // *** Works with this hack. Without exception block it // fails with SelectedValue could not be assigned. With it // re-binding works. // // this fails for some unknown reason on rebinds (off a btn submission) // which in effect rebinds an already bound list. // // First pass through always works fine - only rebind fails. // // complains about SelectedValue being set to a value // that doesn't exist even though selection is already // cleared AND the value that it actually is set after // DataBind() DOES exist in the data. } if ( !string.IsNullOrEmpty(this.ResourceSet) ) this.lstResourceSet.SelectedValue = this.ResourceSet; else { if (this.lstResourceSet.Items.Count > 0) { this.ResourceSet = this.lstResourceSet.Items[0].Value; this.lstResourceSet.SelectedValue = this.ResourceSet; } } }
Tags:
Description:
The code above blows up when the data is bound a second time. Once it gets loaded in OnInit(), then it may be rebound by one of a couple of operations on the page that reload the resource sets.
Note that the code EXPLICITLY clears the selection before databinding.
Full code for this page (and full project) can be found here if anybody is interested:
http://www.west-wind.com:8080/svn/WestwindWebToolkit/trunk/Westwind.GlobalizationWeb/LocalizationAdmin/LocalizationAdmin.aspx.cs
Note that the code EXPLICITLY clears the selection before databinding.
Full code for this page (and full project) can be found here if anybody is interested:
http://www.west-wind.com:8080/svn/WestwindWebToolkit/trunk/Westwind.GlobalizationWeb/LocalizationAdmin/LocalizationAdmin.aspx.cs
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

