0

Behind the code C#, when the user select 3(dropdownlist) then press execute button, it will auto generate 3 textboxes. After user fill out names on 3 textboxes then click request button, I want the 3 names that user entered display on different result textbox. How do I do that?

Here are C# codes,

protected void ExecuteCode_Click(object sender, EventArgs e)
{    

     int amount = Convert.ToInt32(DropDownListIP.SelectedValue);

            for (int num = 1; num <= amount; num++)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                TextBox t = new TextBox();
                t.ID = "textBoxName" + num.ToString();
                div.Controls.Add(t);
                div1.Controls.Add(div);
            }

            ButtonRequest.Visible = true;
}

    protected void ButtonRequest_Click(object sender, EventArgs e)
        {
            string str = "";
            foreach (Control c in phDynamicTextBox.Controls)
            {
                try
                {
                    TextBox t = (TextBox)c;

                    // gets textbox ID property
                    //Response.Write(t.ID);
                    str = t.Text;
                }
                catch
                {

                }
            }

            TextBoxFinal.Text = str;

        }

Then HTML codes,

<div id="div1" runat="server">
        <asp:PlaceHolder ID="phDynamicTextBox" runat="server" />
        </div>
1
  • 1
    If at all possible, just don't create and add controls dynamically. Use a Repeater or DataGrid to create the content off of a template and bind something to it. It will be much easier to deal with. Commented Nov 21, 2013 at 16:40

2 Answers 2

1

you cannot access to control that create dynamically on postback, but you can try get input value from request like this

protected void ExecuteCode_Click(object sender, EventArgs e)
{    
    List<string> tbids = new List<string>();
    int amount = Convert.ToInt32(DropDownListIP.SelectedValue);

        for (int num = 1; num <= amount; num++)
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            TextBox t = new TextBox();
            t.ID = "textBoxName" + num.ToString();
            div.Controls.Add(t);
            phDynamicTextBox.Controls.Add(div);
            tbids.Add(t.ID);
        }
        Session["tbids"] = tbids;
        ButtonRequest.Visible = true;
}

protected void ButtonRequest_Click(object sender, EventArgs e)
    {
        string str = "";
        var tbids = (List<string>)Session["tbids"];
        foreach (var id in tbids)
        {
            try
            {
                str += Request[id]+" "; //here get value tb with id;
            }
            catch
            {

            }
        }

        TextBoxFinal.Text = str;

    }
Sign up to request clarification or add additional context in comments.

Comments

1

One option is: when you create the textbox you save the Id in a list in session, then you through the list and use it:

TextBox myTextbox = (TextBox)FindControl("name");

example:

    List<string> list = (List<string>)Session["myList"];
    TextBox myTextbox;
    foreach (string item in list)
    {
        myTextbox = (TextBox)FindControl(item);
        //in myTextbox you have the atribute Text with the informatcion 
    }

Sorry for my english.

5 Comments

What is not clear? You need save the textbox id in a session variable, and then you need read this session and get the Text of the textbox controls. Tell me what you dont understand and I explain to you my idea :) (or I write all code)
@Nacho do you check that? :-)
I do not understand your question :S. I wanted to modify the code of the question and accidentally copy your code ... I just turned to leave as it was. sorry!
@Nacho when controls added dynamically on postback FindControl will likely return null
@Grundy I tried it, but I had put the code on the load event, so this worked. You're right, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.