Subject: addhandler catch 22
Posted By: roog Post Date: 2/20/2006 8:37:42 AM
Hi,

I have an (in advance) unknown number of dropdownlists in an arraylist. I add a handler like this:

AddHandler arraylist(i).SelectedIndexChanged, AddressOf SomeDropDownList_SelectedIndexChanged

Now I want to make the following event handler:

Private Sub SomeDropDownList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   DoSomething()
End Sub


Which does not work, because I have to define which dropdown it handles (!!??)

handles a_Specific_dropdown.SelectedIndexChanged.

But if I have to define which dropdown this event handles, what use is it to generate dropdowns at runtime (based on a database query)? Can't I define just one event handler, and let it handle all (new and unknown untill runtime) dropdownlists? This one is driving me crazy for a long time now... could somebody please help me?

Roog







Reply By: savan_thakkar Reply Date: 2/20/2006 11:00:49 AM
Hi Friend,Really interesting question.
I spent a lot of time on it.I made the code in C#.net as below.
I wrote this code in Page_Load event.If you try to write it in any other event like Button_Click...then it won't work.If I m correct,you can register an event in only page_Initialize & page_Load event.

int no;            
no=5;//Any other number from database
DropDownList d;
string s;
for(int i=0;i<no;i++)
{
    d=new DropDownList();
    d.Items.Add(new ListItem("AA","A"));
    d.Items.Add(new ListItem("BB","B"));
    d.ID="d" + i.ToString();
    d.AutoPostBack=true;
    d.SelectedIndexChanged+=new EventHandler(d_SelectedIndexChanged);
    p1.Controls.Add(d);
}
private void d_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write("Gone");
}

Give me your feedback as soon as possible

Savan
Reply By: roog Reply Date: 2/20/2006 12:01:00 PM
Many thanks!!! It still does not work in page_load somehow... I just never tried to do it in Page_Init, assuming that it just didn't work. But in Page_Init it does work...

Thanks Savan!


Go to topic 40299

Return to index page 365
Return to index page 364
Return to index page 363
Return to index page 362
Return to index page 361
Return to index page 360
Return to index page 359
Return to index page 358
Return to index page 357
Return to index page 356