Solved

ASP.net dynamically created dropdown list changes event handler by its self

Posted on 2011-09-22
6
410 Views
Last Modified: 2012-08-13
Hi

This is a really strange on (to me anyway).  
Basically I have a page on which I’m creating controls dynamically in a panel.  For each control I create I put it in its own content place holder (this has solved some of my problem, but not all).
I am looping through a query and creating 2 dropdown list for each result DDL1_variable and DDL2 _variable.  Currently there are 2 results so I get 2 sets of 2 dropdown lists.
I have wired up different event handlers to DDL1 and DDL2.  In the event hander I am simply setting a viewstate to a value so I know which one was changed.  So if DDL1 is changed the viewstate(“DDLChanged”) = “DDL1” and if DDL2 is changed viewstate(“DDLChanged”) = “DDL2”. I then set a textbox to = the viewstate at prerender.

Ok sounds simple?  Yeah it should be, and if I click on DDL1_1  (where the _1 is the first appearance of this ddl) the textbox changes correctly to DDL1.  I then change DDL2_1 and the textbox changes correctly to DDL2.  I change DDL1_1  again and the textbox goes back to DDL1.
Ok so this works fine, the problem occurs when I change DDL1_2, the textbox correctly changes to DDL1 and the same correct behaviour for DDL2_2, but now if I change DD2_1 the textbox says DDL1??
I have no idea how changing DDL1_2 could affect the change event of DDL2_1, its using the same handler as DDL1_1 and this does not change it.
This is driving me insane!
Please please help!

Thanks

Brasso
0
Comment
Question by:brasso_42
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36580953
My guess is you are doing something like this:

DropDownList ddl1 = new DropDownList();
DropDownList ddl2 = new DropDownList();

for(i=0; i<10; i++)
{
     ddl1.ID = "ddl1_" + i.ToString();
     myContainer.Controls.Add(ddl1);
     ddl2.ID = "ddl2_" + i.ToString();
     myContainer.Controls.Add(ddl2);
}

Open in new window


what you need to do is:

DropDownList ddl1 = new DropDownList();
DropDownList ddl2 = new DropDownList();

for(i=0; i<10; i++)
{
     //Add new declarations
     ddl1 = new DropDownList();
     ddl2 = new DropDownList();

     ddl1.ID = "ddl1_" + i.ToString();
     myContainer.Controls.Add(ddl1);
     ddl2.ID = "ddl2_" + i.ToString();
     myContainer.Controls.Add(ddl2);
}

Open in new window

0
 
LVL 1

Author Comment

by:brasso_42
ID: 36581050
Hi

I've created an example to help show you and it frigin works ...arrrrrr

Let me back trace and see if I can figure this out.

Thanks for the advice though.

Brasso
0
 
LVL 1

Author Comment

by:brasso_42
ID: 36581621
Hi

Ok I've found that this does not happen if I load my controls in INT not LOAD, however at this point in the page life cycle I cant load my dynamic controls based on values of static controls as they have not loaded yet.

I presume, please correct me if I'm wrong, that I will have to load all controls in INT then hide the ones I dont want at PreRender?

Is this correct?

Thanks

Brasso

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36582073
No, you can load the controls in the Load method, just make sure you re-populate that data each time but DO NOT set the selected value unless

if(!Page.IsPostBack)
    //Set value here
0
 
LVL 1

Author Comment

by:brasso_42
ID: 36586498
well they dont work properly if I load them in LOAD.

I've resolved my issue by reloading all dynamic controls in INT.

I'm, sure I've read somewhere is microsoft recomended practice to do this, it does howevre make life a lot harder, but hay it works so...

Thansk for you help any way. I'll give you the points.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36587085
The simple answer, as you pointed out, is the page life cycle. The control values aren't loaded from ViewState until after Init and before Load.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now