Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 .NET4 ASP.NET4.0
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 4-Aug-12 5:04am
Comments
Wes Aday at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
In button1 click event add the TextBox like:
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:
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
  Permalink  
Comments
Prafulla Sahu at 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 at 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 at 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 at 6-Aug-12 1:19am
   
Is it working now?
Prafulla Sahu at 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
Ajaaz2 at 20-Oct-14 7:40am
   
how it is working? i have tried it. and i got error.
_Amy at 6-Aug-12 0:35am
   
Hi,
The best way to add the controls is in Page_Init event. Because this time only the controls will be created.
Try this:
protected void Page_Init(object sender, EventArgs e)
{
TextBox t1 = new TextBox(); t1.ID = "TextBox2";
TextBox t2 = new TextBox(); t2.ID = "TextBox3";
t1.EnableViewState = true;
t2.EnableViewState = true;
t1.ViewStateMode = ViewStateMode.Enabled;
t2.ViewStateMode = ViewStateMode.Enabled;
Panel1.Controls.Add(t1); Panel1.Controls.Add(t2);
t1.Visible = false;
t2.Visible = false;
}
 
protected void Button1_Click(object sender, EventArgs e) {
TextBox t1 = (TextBox)Panel1.FindControl("TextBox2");
TextBox t2 = (TextBox)Panel1.FindControl("TextBox3");
t1.Visible = true;
t2.Visible = true;
}
 
protected void Button2_Click(object sender, EventArgs e) {
//FnAddControls();
TextBox t1 = (TextBox)Panel1.FindControl("TextBox2");
TextBox t2 = (TextBox)Panel1.FindControl("TextBox3");
}
 

All the best.
Member 10053645 at 28-May-13 1:09am
   
But in my case i have id's also dynamic . Here you are giving manually.
How to do that

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 31 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100