1

I have a web form which dynamically loads controls upon selection in combobox(devexpress). I have the following code on main form

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
        if (Session["_active_control"] != null)//persist control on postbacks
        {
            Control cntrl = Session["_active_control"] as Control;
            pnl_main.Controls.Clear();
            pnl_main.Controls.Add(cntrl);
        }
    }

    protected void cmb_control_SelectedIndexChanged(object sender, EventArgs e)
    {
        Control cntrl= Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
        pnl_main.Controls.Clear();
        pnl_main.Controls.Add(cntrl);
       Session["_active_control"] = cntrl;
    }

also I have a user control having three Textboxes and a button having code

  protected void btn_save_Click(object sender, EventArgs e)
    {
        lbl.Text = ASPxTextBox1.Text + "<br>" + ASPxTextBox2.Text + "<br>" + ASPxTextBox3.Text;
    }

My problem is that the save button of user control is not firing if i load it dynamically (I have checked using breakpoints and also the code shown above. however it runs smoothly if I use it statically.(i.e. by dragging in design mode)

nunespascal
  • 17,584
  • 2
  • 43
  • 46
Akshita
  • 849
  • 8
  • 15

2 Answers2

1

You are right that you have to persist the control across postbacks.

However the Page Load event is too late to add back your controls. Do this on the Init event of your page and you should be good. To receive a postback event, the control should be present when ProcessPostData(called before PreLoad) is called.

Also for textboxes you will want to receive the values entered by the user. This too happens on ProcessPostData, if you add you control after that, you will not receive the values entered by the user.

Refer: ASP.NET Page Life Cycle

nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • thanks for your post , i have put my dyncamic control code on 'OnPreInit' but i am still not able to recieve events on user control. – Akshita Dec 24 '12 at 10:02
  • The dx that you mentioned in the comments above seems like a devExpress custom control. If that control is internally being created on a later event, you will not be able to create it dynamically like this. Most control providers have tutorials about how to do this(the requirement being fairly common). You should ask devExpress support. I will tag the question as devexpress, that may get you some help from developers who know about it. – nunespascal Dec 24 '12 at 10:20
  • Add a static ID to your control. `cntrl.ID = "myLoadedControl"`. Just in case a dynamic id is causing the problem. – nunespascal Dec 24 '12 at 10:50
  • i did that. still the same problem – Akshita Dec 24 '12 at 12:10
0

hey i found the solution

instead on creating the controls in combobox_selectedindexchanged i put my control creation code on Pageload based in combobox.selectedindex i.e.

 protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (cmb_control.SelectedItem != null)
        {
            Control cntrl = Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
            cntrl.ID = "_new_ctrl" + cmb_control.SelectedItem.Value;

            pnl_main.Controls.Clear();
            pnl_main.Controls.Add(cntrl);
        }

    }

see Button click event not firing within use control in ASP .Net

Community
  • 1
  • 1
Akshita
  • 849
  • 8
  • 15