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

Discussion in 'ASP .Net Web Controls' started by haiwen, Oct 23, 2003.

  1. haiwen

    haiwen Guest

    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
     
    haiwen, Oct 23, 2003
    #1
    1. Advertising

  2. 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.
     
    Jacob Yang [MSFT], Oct 24, 2003
    #2
    1. Advertising

  3. haiwen

    haiwen Guest

    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:
    >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.
    >
    >.
    >
     
    haiwen, Oct 24, 2003
    #3
  4. haiwen

    haiwen Guest

    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:
    >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.
    >
    >.
    >
     
    haiwen, Oct 24, 2003
    #4
  5. 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.
     
    Jacob Yang [MSFT], Oct 25, 2003
    #5
  6. haiwen

    haiwen Guest

    Hello,Jacob Yang :

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

    Best wishes,

    haiwen
    >-----Original Message-----
    >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.
    >
    >.
    >
     
    haiwen, Oct 27, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liqun Xu

    Click-Event by dynamical Button

    Liqun Xu, Dec 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    394
    Munsifali Rashid
    Dec 5, 2003
  2. Mythran
    Replies:
    2
    Views:
    545
    Mythran
    Jan 22, 2005
  3. MARTIN Herve \(EXT\)

    Dynamical update of the CLASSPATH

    MARTIN Herve \(EXT\), Nov 5, 2003, in forum: Java
    Replies:
    4
    Views:
    364
    MARTIN Herve \(EXT\)
    Nov 6, 2003
  4. benny
    Replies:
    0
    Views:
    272
    benny
    Jan 11, 2007
  5. Replies:
    9
    Views:
    143
    Evertjan.
    Jun 3, 2007
Loading...

Share This Page