Ashok
Ashok

Reputation: 61

Retrieving values from dynamically created controls Label in asp .net

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


}

Upvotes: 3

Views: 1822

Answers (3)

Glia
Glia

Reputation: 381

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.

Upvotes: 0

Faraz Ahmed
Faraz Ahmed

Reputation: 1607

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.

Upvotes: 1

mck
mck

Reputation: 988

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.

Upvotes: 0

Related Questions