Tuesday, 29 January 2013 13:54

how to create button and click event handler dynamically asp.net

Written by 
Rate this item
(0 votes)

It's a very common requirement to create different types of button and
it's click event handler dynamically in asp.net application.

following codes shows how to do that properly -

LinkButton but = new LinkButton();
but.Text = "csharp";
but.Attributes.Add("modid", ModuleId.ToString());
but.ID = ModuleId.ToString();//used dnn module id
but.BorderColor = Color.Black;
but.BackColor = Color.White;
but.BorderWidth = Unit.Pixel(1);
but.CssClass = "Mitsol_Paging_Button";
but.Style.Add("margin-left", "3px");
but.Style.Add("padding", "2px 3px 2px 3px");
but.OnClientClick = "MitsolPageIndex_Click(this);";
but.Click += new EventHandler(Page_Command);

pagePlaceholder.Controls.Add(but);

here pagePlaceholder is a PlaceHolder control and Page_Command
is the event handler registered.It will be look like -

protected void Page_Command(object sender, EventArgs e)
{
//code goes here
}

Read 3337 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.