By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

How do I fetch textbox value from dynamically created textbox

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.