how to get the dynamical control's content from server side

H

haiwen

hello, everyone:

I need to programmatically generate web control on a page.
such as:
for(int i=0;i<4;i++)
{
Label l=new Label();
l.Text="name "+i;
TextBox t=new TextBox();
t.ID=i.ToString();
t.Text="txt "+i;
Label l2=new Label();
l2.Text="<br>";
PlaceHold1.Controls.Add(l);
PlaceHold1.Controls.Add(t);
PlaceHold1.Controls.Add(l2);
}
but I can not get the content of those TextBox on server
side. could anyone help?

haiwen
 
J

Jacob Yang [MSFT]

Hi HaiWen,

Based on my research and experience, we can use the FindControl method.
Please refer to the following code snippet.
...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
TextBox myText = new TextBox ( );
myText.Text = "Welcome to ASP.NET";
myText.ID = "myText";
PlaceHolder1.Controls.Add ( myText );
myText.Attributes.Add("style",@"Z-INDEX: 103; LEFT: 16px; POSITION:
absolute; TOP: 128px");

}

...
}

private void Button2_Click(object sender, System.EventArgs e)
{
TextBox1.Text = ((TextBox)(PlaceHolder1.FindControl("myText"))).Text;
}
...

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
H

haiwen

hello,Jacob Yang :

Thanks so much for your help.

haiwen
-----Original Message-----
Hi HaiWen,

Based on my research and experience, we can use the FindControl method.
Please refer to the following code snippet.
...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
TextBox myText = new TextBox ( );
myText.Text = "Welcome to ASP.NET";
myText.ID = "myText";
PlaceHolder1.Controls.Add ( myText );
myText.Attributes.Add("style",@"Z-
INDEX: 103; LEFT: 16px; POSITION:
 
H

haiwen

Hello Jacob Yang :

one more question is if there several kinds of controls in
placehold, but I just want to get the text of textbox
control. I use:
foreach(Control b in PlaceHold1.Controls)
{
if(b.GetType()==Type.GetType("TextBox"))
{
s +="score: "+((TextBox)b).Text+"<br>";
}
}
but I get nothing, Could you tell me what is wrong with it?

Thanks a lot,

haiwen
-----Original Message-----
Hi HaiWen,

Based on my research and experience, we can use the FindControl method.
Please refer to the following code snippet.
...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
TextBox myText = new TextBox ( );
myText.Text = "Welcome to ASP.NET";
myText.ID = "myText";
PlaceHolder1.Controls.Add ( myText );
myText.Attributes.Add("style",@"Z-
INDEX: 103; LEFT: 16px; POSITION:
 
J

Jacob Yang [MSFT]

Hi Haiwen,

Please refer to the following code snippet based on the sample in my
previous post.

...
private void Button2_Click(object sender, System.EventArgs e)
{
//TextBox1.Text = ((TextBox)(PlaceHolder1.FindControl("myText"))).Text;
foreach(Control b in PlaceHolder1.Controls)
{
if(b.GetType().ToString() =="System.Web.UI.WebControls.TextBox")
{
TextBox1.Text = ((TextBox)(b)).Text;
}
}

}
...

Does it answer your question? If I have misunderstood your conern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
H

haiwen

Hello,Jacob Yang :

Thank you very much for your help. It solves my problem.

Best wishes,

haiwen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top