Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use This Code at Aspx:
XML
<asp:Button ID="BtnAdd" runat="server" Text="Add" onclick="BtnAdd_Click"  />

       <asp:Button ID="BtnTake" runat="server" Text="TakeValue"
           onclick="BtnTake_Click"  />

       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

       <asp:Panel ID="Panel1" runat="server">

       </asp:Panel>


And At aspx.cs:
protected void BtnAdd_Click(object sender, EventArgs e)
    {
        Label lbl = new Label();
        lbl.ID = "Lbl1";
        lbl.Text = "Arun";
        Panel1.Controls.Add(lbl);
    }
    protected void BtnTake_Click(object sender, EventArgs e)
    {
        string lblName = "Lbl1";
        //Label Lbl = Panel1.FindControl(lblName) as Label;
        Label Lbl = (Label)Panel1.FindControl(lblName);
        TextBox1.Text = Lbl.Text; 
    }



When i click add button i easily create one Dynamic label,

but when i click on second button(take value)

Then it generated An error: like this:
Object reference not set to an instance of an object.


when i try Break point then i see that After click on button 2,
Label Lbl = (Label)Panel1.FindControl(lblName);


lbl return Null:

So what can i do for taking that dynemic created lables values
Posted
Updated 8-Apr-13 1:48am
v2

Hi Arun,


After second postback (BtnTake_Click), your dynamically added controls will be lost. so you should add them in page's init event not in first button's click event.


Don't forget to set autopostback property of dropdownlist to true.
 
Share this answer
 
Always create your dynamic control in Page_Init event (Read this[^] for a detailed information).
Change your CS. Try this:
C#
//Create the controls in this page event
protected void Page_Init(object sender, EventArgs e)
{
    Label lbl = new Label();
    lbl.ID = "Lbl1";
    lbl.Text = "Arun";
    lbl.Visible = false; //Hide the control
    Panel1.Controls.Add(lbl);
}

protected void BtnAdd_Click(object sender, EventArgs e)
{
     Label Lbl = (Label)Panel1.FindControl(lblName);
     if(lbl != null)
          lbl.Visible = true;//Show if need
}
protected void BtnTake_Click(object sender, EventArgs e)
{
    string lblName = "Lbl1";
    //Label Lbl = Panel1.FindControl(lblName) as Label;
    Label Lbl = (Label)Panel1.FindControl(Lbl1);
    TextBox1.Text = Lbl.Text; 
}


--Amit
 
Share this answer
 
v3
Comments
Arun kumar Gauttam 8-Apr-13 8:31am    
your solution work fine, in some condition, but when i want to bind data on dropdown selected event then, it not work,

Because At Page_Init Event, Dropdown value is always null or blank,
whether i select some value in drop down
_Amy 8-Apr-13 8:33am    
Don't bind it in init event. You can do it in Page_Load event or any other events.
Arun kumar Gauttam 8-Apr-13 8:47am    
thanks sir,now it work perfectly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900