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;
    }


}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 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
but Page_Load/postback called first, how do I know it is on that button click ?
here's the solution of your problem. stackoverflow.com/questions/18798940/…
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 !
|
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.

1 Comment

Thanks for the response
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.