i want to create dynamic textbox control and add in panel and store its value in database
i have already create dynamic textbox control and add in panel but i am not able to get textbox value in Button Click
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 13, 2013, 10:43 AM EDT
the asp.net don't keep control state.
So if you want to create control dynamicly, you should create the dynamic control every time in Page_Load function.
You can store the dynamic control into session, and then read it at page_load function.
Here is a example, maybe help you. i modified your code check it.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["txt"] != null)
{
TextBox txt = Session["txt"] as TextBox;
panel1.Controls.Add(txt);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
txt.Text = ""; txt.ID = "TextBox10";
txt.Width = 560; txt.Height = 48;
panel1.Controls.Add(txt);
Session["txt"] = txt;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox t1 = (TextBox)panel1.FindControl("TextBox10");
lblshow.Text = "Text is " + t1.Text;
}
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 12:12 AM EDT
Suppose it's called txtDynamic and it's within a panel called panel1.
Then you can access it from within a button_Click handler as follows:
TextBox txtDynamic = this.panel1.Controls["txtDynamic"] as TextBox;
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 13, 2013, 12:13 AM EDT
Hi,
Refer to this link
http://www.dotnetspider.com/resources/27690-Get-Text-Dynamically-Created-Text-Box.aspx
http://stackoverflow.com/questions/1487618/accessing-value-of-dynamically-created-controls-c-asp-net
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 13, 2013, 12:15 AM EDT
You can use FindControl for panel and read textbox value
Regards,
Iftikar
Neeraj SinghRank #5,010 · 315 reputationPosted Jul 13, 2013, 3:44 AM EDT
for (int i = 0; i < 2; i++)
{ TextBox s = new TextBox();
s.ID = "tb" + i.ToString();
panel1.Controls.Add(s); }
Get Value
for (int i = 0; i
string txtvalue = "txtBox" + i.ToString();
TextBox txt = (TextBox)paneldyn.FindControl(txtvalue);
}
But Get Value is Not working ....
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 3:50 AM EDT
The code to access the TextBoxes should therefore be similar to what Iftikar posted:
Neeraj SinghRank #5,010 · 315 reputationPosted Jul 13, 2013, 4:05 AM EDT
{
TextBox txt = new TextBox();
txt.Text = ""; txt.ID = "TextBox10";
txt.Width = 560; txt.Height = 48;
panel1.Controls.Add(txt);
}
protected void btnShow_Click(object sender, EventArgs e)
{
TextBox t1 = (TextBox)panel1.FindControl("TextBox10");
lblshow.Text = "Text is " + t1.Text;
}
Again t1 value is null
VulpesRank #55 · 29.2K reputationPosted Jul 13, 2013, 5:06 AM EDT
Neeraj SinghRank #5,010 · 315 reputationPosted Jul 13, 2013, 10:39 AM EDT