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

    Dynamically add label in

    I have created one page .On click of button I am showing result of textbox1, textbox2 and dropdownlist in one label control.Label control is dynamically created on buttonclick event and showing inside table control.It is working fine.But user can click many no of time and data of textbox and dropdownlist is different.I want to show all.When user first time click show only one label and it's data.When user change info of dropdownlist and textboxes,and click on button it suppose to 2 label with previous value on first label and second label contain latest value.How to show that?Every time new one is replacing with old one and showing only one label at a time

    //ASPX file
    //Textbox1 Textbox2 Dropdownlist
    // Table

    // Code behind
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    Label lb = new Label();
    lb.ID ="lbl"+ ViewState["Count"].ToString();

  • #763898
    Before creating the control check whether that same ID is there.

    If it is there assign only the value.
    If it is not there create a control and assign the value.

    By Nathan
    Direction is important than speed

  • #763907
    Hai Pinky,
    I think, you need to persist the control when created for the first time.
    This you can do either by inserting in to the database and then retrieving it back so that the control and its values can be re-populated with the new control values.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #763918

    Dont create the dynamic label inside the button click.
    Create the label and keep it as visible=false.
    Then in button click assign the values for the label and make the visible property of label to true,
    which is already created on pageload.

    protected void Page_Load(object sender, EventArgs e)

    Label1.Visible = false;

    protected void btn_Click(object sender, EventArgs e)
    Label1.Visible = true;
    Label1.Text="your table data"; // concatenate all values in to one string variable and assign it to label1.Text.

    Sridhar Thota.
    Editor: DNS Forum.

  • #763924

    Can you try this code

    Client side

    <form id="form1" runat="server">
    <asp:TextBox ID="Txt1" runat="server"></asp:TextBox>
    <asp:Button ID="bt1" runat="server" Text="Submit" OnClick="bt1_Click" />

    Server Side Code

    Label lbl = new Label();
    lbl.ID = "lbl1";
    lbl.Text = Txt1.Text;

    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

Sign In to post your comments