Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I had ten text boxes with ten ID's. These are all created dynamically. I need to give some values to each text box on button click,
How can i give values? Can any one help me please.

Thanks in advance.
Posted
Updated 18-Dec-10 1:25am
v2

How about something like this:
C#
protected void Button1_Click ( object sender, EventArgs e )
{
    IEnumerable<TextBox> tboxes = this.Controls.OfType<TextBox> ();
    TextBox txtbx = tboxes.First<TextBox> ( t => t.ID == "txt1" );
    txtbx.Text = "something";
}
 
Share this answer
 
Please try:

In aspx.cs:
C#
public void addButton()
    {
        //setText() is a javasript function to set to 10 textboxes, examples
        /*
         * function setText()
         * {
         *      txt0.value = 'textbox0';
         *      txt1.value = 'textbox1';
         *      txt2.value = 'textbox2';
         *      txt3.value = 'textbox3';
         *      //Etc...
         * }
         */
        Response.Write("<input type="button"  önclick="setText();" value="Click Me" /> <br />");
    }
    
    public void addTextBoxes()
    {        
        for (int i = 0; i < 10; i++)
        {
            Response.Write("<input id="txt" + i.ToString() + "" type="textbox" /> <br />");
        }        
    }

In aspx:
<head  runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function setText(){
            form1.txt0.value = 'textbox0';
            form1.txt1.value = 'textbox1';
        }
    </script>
</head>
<body>
    <form id="form1"  runat="server">
        <%addButton(); %> 
        <%addTextBoxes(); %>
    </form>
</body>

Regards.

Andy
 
Share this answer
 
v3
Comments
TweakBird 19-Dec-10 22:58pm    
Please add <pre> tag code
[textboxID].Text = "whatever";

Put this in button click event handler.


Edit: Did not saw ASP.Net tag. :doh:

Since these are dynamically created controls, AFAIK you will have to recreate them on post back. While recreating, you can check if the postback is because of button click and if yes, set the text.
 
Share this answer
 
v2
Comments
tulasiram3975 18-Dec-10 6:43am    
not work sir
i have created only with one TextBox class
like
for(i=0;i<10;i++)
{
TextBox txt=new TextBox()
txt.id="txt"+i;
controls.add(txt);
}


}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900