0

I am generating dynamic html controls. The controls are being generated on Init, after the user has caused a postback by pressing a button. However, he needs to press it twice so that the controls are generated. How can I fix this? Code:

 protected override void OnInit(EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (Session["id"] != null)
            {
                string id= Session["id"].ToString();
                GenerateDynamicControls(id);

            }
        }
    }

 protected void Page_Load(object sender, EventArgs e)
    {
        Session["id"] = null;
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        string id = TextBox1.Text;
        Session["id"] = id;
    }
enb081
  • 3,831
  • 11
  • 43
  • 66
  • You're only creating the controls if it's a postback explicitly. Stop telling it to wait until a postback and it will stop waiting for a postback.. What's the confusion? – Kieren Johnstone Mar 19 '13 at 10:20
  • @KierenJohnstone I need to create controls after the postback of the button, but it needs **two** postbacks in order to work. – enb081 Mar 19 '13 at 10:22
  • 1
    Set a breakpoint in OnInit and see why the code isn't called? – Kieren Johnstone Mar 19 '13 at 10:23

4 Answers4

1

Session["id"] is set to null on page load. When the page is posted back after button click, the OnInit method is called first and it get the value of Session["id"] as null. After that the button click event is executed and Session["id"] is set. So when you click the button second time the OnInit has the value other than null for Session["id"] and your code is executed on the second click.

Ruchit Rami
  • 2,273
  • 4
  • 28
  • 53
  • `OnInit` is called before `PageLoad` therefore it gets the Session variable before it gets null. – enb081 Mar 19 '13 at 10:29
  • Yes, but on first click of button the `Session["id"]` is not set before `OnInit` because `Button1_Click` is not called before `OnInit`. – Ruchit Rami Mar 19 '13 at 10:31
  • Right.Then, how can I fix it? – enb081 Mar 19 '13 at 10:33
  • You can not use `OnInit` if you are setting `Session["id"]` with button click back end event. You can set it in the method `OnInit` itself if your logic permits. – Ruchit Rami Mar 19 '13 at 10:37
1

Call GenerateDynamicControls(id); when button is clicked. That way you will have your controls on first click. And when page reloads they will be recreated in OnInit.

protected void Button1_Click(object sender, EventArgs e)
{
    string id = TextBox1.Text;
    Session["id"] = id;
    GenerateDynamicControls(id);
}
alex
  • 12,464
  • 3
  • 46
  • 67
  • The problem is that I generate controls as strings and then I append them to a div, for instance: `div1.InnerHtml += html;`. This way they will be appended twice? – enb081 Mar 19 '13 at 10:31
  • Why don't you want to add them as a child controls? Anyway, you can have a flag in that checks whenever controls are already generated, so as not to generate them for the second time. – alex Mar 19 '13 at 10:34
  • It's a very complicated situation but the main reason is that I don't want them to run on Server because I am manipulating them later with ajax requests, etc – enb081 Mar 19 '13 at 10:47
1

There is no need to use session for these purposes. Plus your code might fail after subsequent postbacks.

protected override void LoadViewState(object state)
{
    base.LoadViewState(state);
    var id = this.ViewState["DynamicControlGeneration"] as string;
    if (id != null)
        GenerateDynamicControls(id);
}

protected void Button1_Click(object sender, EventArgs e)
{
    string id = TextBox1.Text;
    this.ViewState["DynamicControlGeneration"] = id;
    GenerateDynamicControls(id);
}
Knaģis
  • 20,827
  • 7
  • 66
  • 80
  • 1
    @Knagis This seems to work fine, but can I add the condition `if ((this.ViewState["DynamicControlGeneration"] == null) || (this.ViewState["DynamicControlGeneration"].ToString() != id))` before generating controls to avoid repetition? – enb081 Mar 19 '13 at 10:43
  • wouldn't a better solution be to disable the button? – Knaģis Mar 19 '13 at 10:46
  • He can write on the TextBox another Id and generate other controls. – enb081 Mar 19 '13 at 10:47
  • Implemented in my much complicated situation and it works very well. thanks – enb081 Mar 19 '13 at 11:11
1

Try this,

protected override void OnInit(EventArgs e)
{
     if (Page.IsPostBack)
     {
         string id = Request.Form[TextBox1.ClientID].ToString();
         GenerateDynamicControls(id);
     }
}
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105