Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TextBox textBox = new TextBox();
textBox.ID = "c_textBox" + i.ToString();

Button bt = new Button();
template1.Controls.Add(textBox);
bt.Text = i.ToString();
bt.Click += new EventHandler(Button_Click);
template1.Controls.Add(bt);
}

}
protected void Button_Click(object sender, EventArgs e)
{
/*wanna do here */
}

i have generated button and textbox relevently and wish to fill the id of buton into its relevent textbox without trigering the other textboxes

like if i press button 4 the textbox with id c_textbox should be 4 without effecting other textboxes

please help
Posted
Comments
[no name] 29-Aug-13 10:10am    
Okay so go ahead and do that. Did you maybe want to explain some sort of a problem?

1 solution

You can create a usercontrol (e.g. UserControl1) with a Textbox (e.g. id TextBox1) and one Button (e.g. id Button1). In the user control you can easily write Button_Click event.
Also, you create a public function SetValue(int i) to set the Text property of the Button

C#
public void SetValue(int i)
{
    Button1.Text = i.ToString();
}


In your page, you can do the following

C#
protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        UserControl1 uc = new UserControl1();
        uc.SetValue(i);
        template1.Controls.Add(uc);
    }
}


Hope it helps
 
Share this answer
 

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