By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

ListBox in dynamically created UserControl loses selected index.

P: 3
okay; ASP.

I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach (Product p in manager.Products)
  3.         {
  4.             ASP.MasterDetailDropDown productMasterDetail;
  5.             productMasterDetail = (ASP.MasterDetailDropDown)LoadControl("~/controls/MasterDetailDropDown.ascx");
  6.             productMasterDetail.ID = p.name + " DropDown";
  7.             productMasterDetail.product = p;
  8.             productMasterDetail.ListBoxID = p.name + "-BuildNumberListBox";
  9.  
  10.             PlaceHolder.Controls.Add(productMasterDetail);
  11.         }
  12.  
as you can see, i set the id of the usercontrol so that viewState events will be able to find it on postback. I'm also setting the id of the listbox on the control; not sure whether i need to but it hasn't helped anyway.

the UserControl is set out like this -

Expand|Select|Wrap|Line Numbers
  1. <%@ Control ClassName="MasterDetailDropDown" Language="C#" AutoEventWireup="true"
  2.     CodeFile="MasterDetailDropDown.ascx.cs" EnableTheming="true" Inherits="MasterDetailDropDown" %>
  3. <div class="ProductDataItem">
  4.     <div class="ProductDataItemHeader">
  5.         <h2>
  6.             <asp:Label ID="mainMasterNameLabel" runat="server" Text="Label"></asp:Label></h2>
  7.         <p>
  8.             <asp:Label ID="subMasterNameLabel" runat="server" Text="Label"></asp:Label></p>
  9.         <span class="showHideButton">
  10.             <asp:ImageButton ID="ShowHideImageButton" runat="server" ImageUrl="~/Images/show.png"
  11.                 OnClick="ShowHideImageButton_Click" />
  12.         </span>
  13.     </div>
  14.     <div class="ProductDataItemDetail">
  15.         <asp:Panel ID="DetailPanel" runat="server" Visible="False">
  16.             <div class="FloatListBoxLeft">
  17.                 <asp:ListBox ID="BuildNumberListBox" runat="server" SkinID="ListBox" AutoPostBack="true" OnSelectedIndexChanged="BuildNumberListBox_SelectedIndexChanged">
  18.                 </asp:ListBox>
  19.             </div>
  20.             <div class="BuildDetails">
  21.                 <ul>
  22.                     <li>Build: 
  23.                         <asp:Label ID="BuildNumberLabel" runat="server" Text="Label"></asp:Label></li>
  24.                     <li>Build Successful: 
  25.                         <asp:Image ID="BuildSuccessfulImage" runat="server" /></li>
  26.                     <li>Build Time/Date: 
  27.                         <asp:Label ID="BuildTimeDateLabel" runat="server" Text="Label"></asp:Label></li>
  28.                     <li>Build Location: 
  29.                         <asp:HyperLink ID="BuildLocationHyperlink" runat="server">HyperLink</asp:HyperLink></li>
  30.                 </ul>
  31.             </div>
  32.             <div class="BVTData">
  33.                 <ul>
  34.                     <li>Pass Rate(%) </li>
  35.                     <li>BVT Completion(%) </li>
  36.                     <li>Number of Critical Issues Rasied </li>
  37.                     <li>Number of Highs Issues Raised </li>
  38.                     <li>Number of Medium Issues Raised </li>
  39.                     <li>Number of Low Issues Raised </li>
  40.                 </ul>
  41.             </div>
  42.         </asp:Panel>
  43.     </div>
  44. </div>
  45.  
My problem is that the listbox in the usercontrol loses ity's selectedindex on postback. All the usual viewstate stuff is correct. Can't figure out why i would lose it all. I have to recreate the controls everytime and i have to repopulate the listbox everytime for obvious reasons but my understanding is that as long as this is done on page_init then the values would be correct.


the code which bind the listbox goes like this -

Expand|Select|Wrap|Line Numbers
  1.  
  2.             mainMasterNameLabel.Text = product.name;
  3.             subMasterNameLabel.Text = "v" + product.Version.ToString();
  4.  
  5.             //Builds ListBox
  6.             foreach (Build b in this.product.builds)
  7.             {
  8.                 ListItem buildListItem = new ListItem(b.BuildNumber.ToString(), b.Id.ToString());
  9.                 BuildNumberListBox.Items.Add(buildListItem);
  10.             }
  11.  
What am i doing wrong?

Thanks in advance.
Sep 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Make sure that your are binging your data when your page is not a postback

Expand|Select|Wrap|Line Numbers
  1. if(!isPostBack)
  2.     ListItem buildListItem = new ListItem(b.BuildNumber.ToString(), b.Id.ToString());
  3.     BuildNumberListBox.Items.Add(buildListItem);
  4.  
Nov 11 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.