473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ListBox in dynamically created UserControl loses selected index.

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
1 3373
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

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

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
1
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
2
by: tangokilo | last post by:
Hello and thanks for your help, I have the following Listbox created in VisualStudio 2003 designer, desiring to select multiple entries from that list: -------------------------------...
6
by: GS | last post by:
how can I set tooTip on ToolTip1 for a listbox?
1
by: =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?= | last post by:
Hi I have been banging my head for a few days now with this issue. I have a page that has several update panels everything is working fine except for 1 thing. I am creating a sort of wizard...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.