?
Solved

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

Posted on 2011-09-22
6
Medium Priority
?
419 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
LVL 28

Accepted Solution

by:
strickdd earned 2000 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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