Solved

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

Posted on 2011-09-22
6
409 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

17 Experts available now in Live!

Get 1:1 Help Now