I created a simple asp button using code behind. I added this button on page successfully and it is showing me on web page but I got a problem when I clicked on the button then after post back the button hide on web page. please help me to resolve this. Here is my code :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CreateButton();
}
}
protected void CreateButton()
{
Button btn = new Button();
btn.ID = "newDynamicBtn";
btn.Text = "Click Me";
//btn.Attributes.Add("runat", "server");
//btn.Attributes.Add("onClick", "newDynamicBtn_Click");
//btn.OnClientClick = "return confirm('are you sure ?')";
btn.Click += newDynamicBtn_Click;
form1.Controls.Add(btn);
}
protected void newDynamicBtn_Click(object sender, EventArgs e)
{
Response.Write(@"<script>alert('Hello')</script>");
}
CreateButtonmethod isn't called when it is aPostBack. Take it outside of thatifcondition and then it will work