3

I have created Label controls dynamically on button click:

protected void createDynamicLabels_Click(object sender, EventArgs e)
{
    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label MyLabel = new Label();
        MyLabel.ID = "lb" + i.ToString();
        MyLabel.Text = "Labell: " + i.ToString();
        MyLabel.Style["Clear"] = "Both";
        MyLabel.Style["Float"] = "Left";
        MyLabel.Style["margin-left"] = "100px";

        Panel1.Controls.Add(MyLabel);
    }
}

When I tried to read back fro another button I see Label Control returned null

Label str = (Label)Panel1.FindControl("lb" + i.ToString());

not sure what went wrong here

protected void bReadDynValue_Click(object sender, EventArgs e)
{

  int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}
Daniel
  • 9,491
  • 12
  • 50
  • 66
Ashok
  • 61
  • 5

3 Answers3

1

this is the issue of every time page load event. ASP.net fire every time page load event when any button is click.

suppose in this example..

protected void Page_Load(object sender, EventArgs e)
{

    if(!IsPostBack)
        createDynamicLabels();
}


private void createDynamicLabels()
    {
        int n = 5;
        for (int i = 0; i < n; i++)
        {
            Label MyLabel = new Label();
            MyLabel.ID = "lb" + i.ToString();
            MyLabel.Text = "Labell: " + i.ToString();
            MyLabel.Style["Clear"] = "Both";
            MyLabel.Style["Float"] = "Left";
            MyLabel.Style["margin-left"] = "100px";

            Panel1.Controls.Add(MyLabel);


        }
    }

protected void bReadDynValue_Click(object sender, EventArgs e)
{

    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}

when Button trigger Page doesn't have any label because it is made on runtime. and Page doesn't find particular label. if you tried above code it is run properly.

 protected void Page_Load(object sender, EventArgs e)
{

        createDynamicLabels();
}


private void createDynamicLabels()
    {
        int n = 5;
        for (int i = 0; i < n; i++)
        {
            Label MyLabel = new Label();
            MyLabel.ID = "lb" + i.ToString();
            MyLabel.Text = "Labell: " + i.ToString();
            MyLabel.Style["Clear"] = "Both";
            MyLabel.Style["Float"] = "Left";
            MyLabel.Style["margin-left"] = "100px";

            Panel1.Controls.Add(MyLabel);


        }
    }

protected void bReadDynValue_Click(object sender, EventArgs e)
{

    int n = 5;
    for (int i = 0; i < n; i++)
    {
        Label str = (Label)Panel1.FindControl("lb" + i.ToString());
        lbGetText.Text = str.Text;
    }


}

in this Example code find label every time because every time it can make labels for this page.

Faraz Ahmed
  • 1,467
  • 2
  • 18
  • 33
  • Thanks, I wanted to do it 2 different buttons to generate dynamic controls and retrieve its values. But due to postback my idea of using 2 buttons not going to work. thanks again – Ashok Mar 03 '16 at 09:05
  • @Ashok Why don't u use button event handler calling in else statement of (!isPostback).. it can work like this.. if you want to do with 2 buttons – Faraz Ahmed Mar 03 '16 at 09:19
  • 1
    but Page_Load/postback called first, how do I know it is on that button click ? – Ashok Mar 03 '16 at 09:26
  • here's the solution of your problem. http://stackoverflow.com/questions/18798940/how-to-check-whether-asp-net-button-is-clicked-or-not-on-page-load – Faraz Ahmed Mar 03 '16 at 09:39
  • I tried that now on page_load I can see button control but Label ID is already null may be because postback is already happened ! – Ashok Mar 03 '16 at 09:51
  • Because label is generated dynamically from code, you want to add labels, every time whenever (isPostBack) is true with buttonClick event triggered. – Faraz Ahmed Mar 03 '16 at 09:54
0

Dynamically created labels exists only until the next postback occurs. When you click on another button to retrieve their values a postback occurs and values become null.

For saving labels state after postback you have to use some hidden field.

mck
  • 978
  • 3
  • 14
  • 38
0

If the text / value of the labes does not change it is enough to generate them on every postback (as mck already mentioned). If you need to retrieve changes made on the client side, you should create the controls in the OnInit event instead of the PageLoad and use inputs / texboxes instead of labels.

Another option (which I would recommend) would be to use a asp:Repeater to generate the Labels.

Glia
  • 371
  • 2
  • 9