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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Jul 13, 2013, 2:43 PM
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;
}
Neeraj SinghPosted Jul 13, 2013, 2:39 PM
VulpesPosted Jul 13, 2013, 9:06 AM
Neeraj SinghPosted Jul 13, 2013, 8:05 AM
{
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
VulpesPosted Jul 13, 2013, 7:50 AM
The code to access the TextBoxes should therefore be similar to what Iftikar posted:
Neeraj SinghPosted Jul 13, 2013, 7:44 AM
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 ....
Iftikar HussainPosted Jul 13, 2013, 4:15 AM
You can use FindControl for panel and read textbox value
Regards,
Iftikar
Sanjeeb LenkaPosted Jul 13, 2013, 4:13 AM
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
VulpesPosted Jul 13, 2013, 4:12 AM
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;