.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Dynamic control creation

Posted By: preetu     Posted Date: March 03, 2010    Points:2   Category :ASP.Net
if create 4 dynamic button control then how can i create their specific click event ?

Author: Lalij Mer          Accepted Answer   
Posted Date: March 03, 2010     Points: 10   

Check this URL


Please Check my answer if you help....
Thank You...

Author: Amit Mehra          Accepted Answer   
Posted Date: March 03, 2010     Points: 10   


To create button you can use this code

protected void Page_PreInit(object sender, EventArgs e)
Button myButton = null;

//Create 5 button dynamically
for (int i = 0; i < 5; i++)
//Create new instance of Button class and assign it necessary property
myButton = new Button();
myButton.ID = "myButton" + i; //Assign Id
myButton.Text = "myButton" + i;

//Attach the button click event handler
myButton.Click += new EventHandler(myButton_Click);

//Add Newly created Button to Panel

//Add berek so the next Button will create in next new line
Literal lit = new Literal();
lit.Text = "</br></br>";
// add the Button to panel in a web page


and click event

void myButton_Click(object sender, EventArgs e)
Button btn = sender as Button;
if(btn !=null)
Response.Write("Button id " + btn.ID + " clicked");


Please "Mark As Answer " if this post help you.
Author: preetu             
Posted Date: March 05, 2010     Points: 5   

Thanks a lot!!!!!

but my doubt is that..
here controls number is not fix..means it is dynamic also...

so how can i create there specific event....

i do something that...

If (CType(sender, Button).ID) = "btn" & i Then

this code gave control's id

so ...????

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend