Solved

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

Posted on 2011-09-22
6
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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