LinkButton_Click event does not fire

I created a dynamic link. I want to go to other pages when the click event fires. But now, when I click the link button, the whole page is cleared and the click event does not fire.

System.Web.UI.WebControls.LinkButton lbView = new System.Web.UI.WebControls.LinkButton(); lbView.Text = "<br />" + "View"; lbView.Click += new System.EventHandler(lbView_Click); tc.Controls.Add(lbView); tr.Cells.Add(tc); protected void lbView_Click(object sender, EventArgs e) { Response.Redirect("contactus.aspx"); } 

Please, help.

+4
source share
2 answers

When you create a dynamic control, you cannot directly create the click event of this control. In your case, you must follow this path. Add javascript to redirect the contactus.aspx page.

 System.Web.UI.WebControls.LinkButton lbView = new System.Web.UI.WebControls.LinkButton(); lbView.Text = "<br />" + "View"; btn.OnClientClick = "return RedirectTo();"; // You need to add javascript event tc.Controls.Add(lbView); tr.Cells.Add(tc); // javascript <script> function RedirectTo() { window.location.href = 'contactus.aspx'; return false; } </script> 

Try it. Hope this works for you.

+1
source

Put your code inside and try: -

 if(!IsPostBack){ System.Web.UI.WebControls.LinkButton lbView = new System.Web.UI.WebControls.LinkButton(); lbView.Text = "<br />" + "View"; lbView.Click += new System.EventHandler(lbView_Click); tc.Controls.Add(lbView); tr.Cells.Add(tc); } protected void lbView_Click(object sender, EventArgs e) { Response.Redirect("contactus.aspx"); } 
0
source

Source: https://habr.com/ru/post/1495679/


All Articles