Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
I have a problem that how would I access dynamic TextBox's data in asp.net.
my requirement is I have a TextBox and 2 button(button1 and button2) when click on button1 dynamically 2 TextBox's should create after that I am entering some values into the TextBox's then when I will click on button2 the 2 TextBox's data should combine and display inside the First Textbox.
Suggest me how would I do.
Thanks.
Posted
Comments
[no name] 4-Aug-12 10:24am    
My suggestion would be to create a button click handler. Then in the click handler create the 2 textboxes. Then in the button 2 click handler, take the data from the 2 textboxes, concatenate the strings and put them in the first textbox.
Prafulla Sahu 4-Aug-12 10:43am    
Thanks Wes ,
I did the way you told but its still the same I am not able to get my result will you please post sample code for me if you will have time,, thanks again for your suggestion Wes.

1 solution

Hi,
In button1 click event add the TextBox like:
C#
TextBox t1 = new TextBox();
t1.ID = "TextBox1";
TextBox t2 = new TextBox();
t2.ID = "TextBox2";
Panel1.Controls.Add(t1);
Panel1.Controls.Add(t2);
//Now your both textboxes are added into panel.
//You can add it wherever you want.

In button2 click event find TextBox value like:
C#
TextBox t1 = (TextBox)Panel1.FindControl("TextBox1");
TextBox t2 = (TextBox)Panel1.FindControl("TextBox2");
//Now you found your both textboxes.
//Get the values using t1.Text and t2.Text



Isn't this too easy.
Anyway try it.
All the best.
--Amit
 
Share this answer
 
Comments
Prafulla Sahu 6-Aug-12 0:06am    
Thanks for your reply but it is still not working for me.I did like this as the way you suggest.two textbox's created when i clicked on button1 but when I am entering some value into those two textbox's and click on button2 it unable to find those two new generated textbox's.I traced the program but when i press button2 the page is reloading so the new generate dynamic controls are also loading for that it value,id every thing vanish I think.try once,thanks once again Amit for your reply
_Amy 6-Aug-12 0:12am    
Where you are writing these codes? Write it inside Not IsPostBack under page load or inside button1 click event. Can you show me your full coding here?
Prafulla Sahu 6-Aug-12 0:16am    
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Button2" />
<asp:TextBox ID="TextBox1" runat="server">


protected void Page_Load(object sender, EventArgs e)
{
}


protected void Button1_Click(object sender, EventArgs e)
{
TextBox t1 = new TextBox();
t1.ID = "TextBox2";
TextBox t2 = new TextBox();
t2.ID = "TextBox3";
Panel1.Controls.Add(t1);
Panel1.Controls.Add(t2);

}

protected void Button2_Click(object sender, EventArgs e)
{
TextBox t1 = (TextBox)Panel1.FindControl("TextBox2");
TextBox t2 = (TextBox)Panel1.FindControl("TextBox3");
}
_Amy 6-Aug-12 1:19am    
Is it working now?
Prafulla Sahu 6-Aug-12 3:39am    
sorry for late ,Thanks Amit its working like a bird, thanks a ton.Only one thing t1.ViewStateMode=ViewStateMode.Enabled i couldn't get this property so i removed it but it is working fine after removed thanks thanks thanks a lot once again

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