You must Sign In to post a response.
  • Category: ASP.NET

    Creating dynamic label controls

    Hi friends,
    I am working on ASP.net 2.0 with C# as cpde behind.I want to create dynamic label control and want to set properties,text to it at page load.as soon as the page loads,i want to see these dynamically created label controls with the property i set to it and those should remain even after a postback.I am not having any other controls(UI) in my page.this page is just to view and postback i mention here is nothing but a refreshing that page.

    The text to be set to that label controls are brought from a datatable.So i dont know in advance that how many records it will fetch and how many controls i want to create.so im planning to create it through a function and calling it in a loop when more than one record is fetched.I get this idea and dont know how to implement it..plz help me..

    thanks in advance...
  • #583904
    Hi Dear please below line of code:

    Label lbl = new Label(); lbl.Text = "Label text"; placeholder.Controls.Add(lbl); // Create a text box control, set the default Text property, and add it to the ControlCollection. TextBox box = new TextBox(); box.Text = "Textbox text"; placeholder.Controls.Add(box);

    Deep Gautam

  • #583907
    write following code in page load event.

    after getting datatable, suppose your datatable object is "dt" then

    foreach(datarow dr in dt.rows)
    string labelname = dr["nameforlabel"].tostring();
    Label newlabel = new Label();
    newlabel.text = "..";
    /*set all property that u want then u can add it to already added panel,or placeholder or table etc. i have directly added to form*/

    forms.controls.add(newlabel);
    }

    there may be some syntax error in above code as i have not run it but u can deal with it.

    maitray bhatt

  • #583927
    try this code


    //after getting dataset write this code
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
    {
    Label lbl = new Label();
    lbl.Text = ds.Tables[0].Rows[i]["name"].ToString();
    //through this you can set property then
    Panel1.Controls.Add(lbl);
    }

    hope this will help you

  • #583941
    Hi All,
    Thank you for all your posts.but plz note already tried these.initially i can create one label control for the first record.In my datatable i've a table with 2 columns and records for it.for each row i need to create 2 label controls and 1st label control is going to hold first cell's value of 1st record and 2nd will hold 2nd cell's value of 1st record..like following
    for(int i=0;i < dt.rows.count; i++)
    {
    Label lb1=new Label();
    lb1.Text=dt.Rows[0][1].ToString();
    Label lb2=new Label();
    lb2.Text=dt.Rows[0][2].ToString();
    }

    But i dont know in advance that how many rows will be loaded in the datatable.My requirement is to create 2 new label controls for every row in the datatable.How to achieve this.
    For this only i posted that if i keep the label creation code inside a function and call it everytime even there is more than 1 record in data table.Plz tell me how to achieve this..

    very urgent..


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.