0

I have a form that contains a dropdownlist, on index changed method,i will call my user control class .cs with parameters choosen by the user, when im putting my code inside the index changed like the code below, it doesnt work, which is a normal behavior:

 protected void ResourceTypesDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        ....
        MyUsercontrol c = new MyUSercontrol(....);
        this.panel.controls.add(c);
    } 

thats why i have to put the code inside my onload method, but the thing is how can i know if it is the ddl that caused the post back? is there a propertie? or should i use page.Request.Params.Get("__EVENTTARGET") technic ? Thanks alot !

Grace
  • 1,265
  • 21
  • 47
  • putting this code in the event handler should work fine, but as you're dynamically adding a control to the tree, you will also have to call this in page init on every *other* postback. It would probably be about a billion times easier to add the user control to the markup and then just toggle the `Visible` property, or use a `Repeater` if you need multiple controls. – Graham Clark Aug 02 '11 at 07:22
  • so i put my code inside the init , and add a repeater, and without the selectedIndexChanged method ? – Grace Aug 02 '11 at 07:27
  • if you need multiple instances of a control, a `Repeater` is an easy way to go. Presumably you're display some data in these controls, maintain this data in a list (e.g. `List`), where `MyData` is a class containing your data. Databind this to the repeater each page load or init. On the `SelectedIndexChanged` event add an item to the list. Store the list in the `Session`. No more fiddly dynamic control code! – Graham Clark Aug 02 '11 at 07:34
  • Alternatively, if you want the dynamic controls route, this is essential reading: http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx – Graham Clark Aug 02 '11 at 07:34
  • @Umar: it depends on my querystring, its not always one user control :) – Grace Aug 02 '11 at 07:37

3 Answers3

2

If your MyUserControl is really user control, that means .ascx file, you should use this:

Page.LoadControl("~/Controls/MyUserControl.ascx")

instead of creating the instance of the control by calling constructor directly.

protected void ResourceTypesDDL_SelectedIndexChanged(object sender, EventArgs e) {
        ....
        var c = Page.LoadControl("~/Controls/MyUserControl.ascx");
        this.panel.controls.add(c); 
}

EDIT:

But of course, after every other post back, you will lose this control. So you should also make sure that you will create all dynamic controls during OnLoad event.

Radek Stromský
  • 838
  • 3
  • 12
  • 27
1

set the property autoPostBack=true on the dropdownlist in order for the page to postback

Or use the below function to get the post back control on the page_load

private string GetPostBackControl()
    {
        string retVal = string.Empty;
        try
        {
            string ctrlname = Page.Request.Params.Get("__EVENTTARGET");
            if (ctrlname != null && ctrlname != string.Empty)
            {
                Control ctrl = this.Page.FindControl(ctrlname);
                if (ctrl != null)
                {
                    retVal = ctrl.ID;
                }
            }
        }
        catch (Exception ex) { ManageException(ex, ShowGeneralErrorMessage); }

        return retVal;
    }
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
  • it is post backing, no prob with that, but when im creating my user contorl inside the indexchanged method , its not workin, when im putting it inside the onload method it is working – Grace Aug 02 '11 at 07:20
  • Ok, then you the above method that returns the server side id of the control to check if this is the drop down list that caused the post back – Ghyath Serhal Aug 02 '11 at 07:23
0

Try setting AutoPostBack="True" property of drop down list. After setting this property when you select an item in list it will automatically do the postback and your event ResourceTypesDDL_SelectedIndexChanged will be fired.

MUS
  • 1,450
  • 4
  • 17
  • 30
  • it is post backing, no prob with that, but when im creating my user contorl inside the indexchanged method , its not workin, when im putting it inside the onload method it is working – Grace Aug 02 '11 at 07:21