Reputation: 171
I have created a dynamic link button. I want to navigate to other pages when the click event is fired . 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.
Upvotes: 4
Views: 91
Reputation: 2145
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.
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.
Upvotes: 1
Reputation: 8871
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");
}
Upvotes: 0