Solved

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

Posted on 2011-09-22
6
413 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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