473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How do I fetch textbox value from dynamically created textbox

Can anyone help me with this problem?
I dynamically creates several textboxes using a placeholder. This works
fine. But is there a way for me to loop through theese textboxes and
retrive its value when clicking a button?

The code I'm working with:

aspx-page
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder ID="EDCTextBoxes" Runat="server" />
<asp:button ID="btnSave" Runat="server" />
</form>

code behind
private void DCP_Init(object sender, EventArgs e) {
CreateTextBoxes();
}
private void CreateTextBoxes(){
this.EDCTextBoxes.Controls.Add(new TextBox());
IterateThroughChildren(this);
}

private void IterateThroughChildren(Control parent){

foreach (Control c in parent.Controls){
if(c.GetType().ToString().Equals("System.Web.UI.We bControls.TextBox")
&& c.ID == null){
((TextBox) c).CssClass = "txtCountryIncrease";
((TextBox) c).ID = UniqueID;
}

if (c.Controls.Count > 0){
IterateThroughChildren(c);
}
}
}

private void btnSave_Click(object sender, EventArgs e){

}

I'm a bit of a newbe at this so please explain to me like you would a
two year old infant :-)

Thanks in advance!

Nov 17 '05 #1
2 8745
Hi,

You can do it with a loop similar to the one you've used in
IterateThroughChildren():

foreach (Control c in parent.Controls)
{
if (c is TextBox) // simpler that what you've done there
{
string name = c.ID;
string value = (c as TextBox).Text; // the cast is essential !!
// do something with name & value
}
}

With the cast, you indicate the compiler that you know that the Control
referenced by 'c' is a TextBox and not any other kind of control.

Regards - Octavio

<ut*******@gmail.com> escribió en el mensaje
news:11*********************@z14g2000cwz.googlegro ups.com...
Can anyone help me with this problem?
I dynamically creates several textboxes using a placeholder. This works
fine. But is there a way for me to loop through theese textboxes and
retrive its value when clicking a button?

The code I'm working with:

aspx-page
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder ID="EDCTextBoxes" Runat="server" />
<asp:button ID="btnSave" Runat="server" />
</form>

code behind
private void DCP_Init(object sender, EventArgs e) {
CreateTextBoxes();
}
private void CreateTextBoxes(){
this.EDCTextBoxes.Controls.Add(new TextBox());
IterateThroughChildren(this);
}

private void IterateThroughChildren(Control parent){

foreach (Control c in parent.Controls){
if(c.GetType().ToString().Equals("System.Web.UI.We bControls.TextBox")
&& c.ID == null){
((TextBox) c).CssClass = "txtCountryIncrease";
((TextBox) c).ID = UniqueID;
}

if (c.Controls.Count > 0){
IterateThroughChildren(c);
}
}
}

private void btnSave_Click(object sender, EventArgs e){

}

I'm a bit of a newbe at this so please explain to me like you would a
two year old infant :-)

Thanks in advance!

Nov 17 '05 #2
Much obliged!

It works like a charm. Maybe I've should have figured that one out my
self.

Thanks again!

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raed Sawalha | last post by:
Hi, This is probably pretty simple but... I am dynamically adding rows to a HtmlTable that contain Textbox controls. (There may be more than one server control/textbox in a given cell) How...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
2
by: JaM | last post by:
Hi all, I have created a gridview vith dynamic textbox columns (they are in variable number, it depends on what things I select from database) aspx code:...
0
by: Silver Oak | last post by:
I have a DataGrid in which one of the columns is TemplateColumn that was created dynamically using iTemplate. I would like to have multi-row editing capability on the DataGrid. I'm trying to...
16
by: mj.redfox.mj | last post by:
Can anyone help? I have a textbox which I'm programatically adding by using the following code: txtTest = New TextBox txtTest.ID = "txtLeft" + cntCount.ToString...
2
by: HHAAPPYY | last post by:
Hi I am trying to pass value of the textbox along with another value to the query string. when i am retriving the txtbox value it always shows me null, my senario is like this On my...
3
by: raghulvarma | last post by:
I have created only one object for the textbox and that particular textbox is being repeated as many times I want.But if I want to add the values in the database from each and every textbox which...
0
by: ahmadbaseet | last post by:
Hello all I am new to VB.NET. I am trying to create dynamically objects and giving them the event handles. Something like this Dim NewTab As New TabPage Dim NewTextBox As New TextBox Dim...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.