简体   繁体   English

LinkBut​​ton_Click事件未触发

[英]The LinkButton_Click event is not firing

I have created a dynamic link button. 我创建了一个动态链接按钮。 I want to navigate to other pages when the click event is fired . 我想在触发click事件时导航到其他页面。 But now, when I click on the link button, the entire page is cleared off and no click event is fired. 但现在,当我点击链接按钮时,整个页面将被清除,并且不会触发任何点击事件。

 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. 请帮忙。

When you are creating dynamic control you can not directly create click event of that control. 在创建动态控件时,无法直接创建该控件的单击事件。 In your case you must follow this way. 在你的情况下,你必须遵循这种方式。 Add javascript to redirect contactus.aspx page. 添加javascript以重定向contactus.aspx页面。

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 this. 试试这个。 Hope it works for you. 希望对你有效。

Put your code inside like this 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");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM