Click Event of dynamically created Link Button is not working
-
Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((
-
Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((
Did you re-bind the events at postback? You would need to.
-
Did you re-bind the events at postback? You would need to.
I donk know how to rebind the event please tell me ..
-
I donk know how to rebind the event please tell me ..
Let the same code run on each postback! You must be skipping the formation of the button and event assignment on consecutive postbacks.
-
Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((
On Pageload event repeat the above code
-
Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((
write this code in protected void Page_PreInit(object sender, EventArgs e) { LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not } private void ctrl_Click(object sender, EventArgs e) { } dont write in protected void Page_Load(object sender, EventArgs e) { }
-
Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((
Check this, it will help you.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
<script >
function jsEvent() {
alert("hai");
}
</script>
</body>
</html>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class dynamicButton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.ID = "Button1";
button.Text = "DynaicButton";
button.Attributes.Add("onclick", "return jsEvent();");
button.Click += new EventHandler(button_Click);
PlaceHolder1.Controls.Add(button);
}
protected void button_Click(object sender, EventArgs e)
{
Response.Write("Server Event: From Dynamic Button");
}
}Raju.M
-
Check this, it will help you.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
<script >
function jsEvent() {
alert("hai");
}
</script>
</body>
</html>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class dynamicButton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.ID = "Button1";
button.Text = "DynaicButton";
button.Attributes.Add("onclick", "return jsEvent();");
button.Click += new EventHandler(button_Click);
PlaceHolder1.Controls.Add(button);
}
protected void button_Click(object sender, EventArgs e)
{
Response.Write("Server Event: From Dynamic Button");
}
}Raju.M
thanks for Helping me. i pasted the code in Page load and it is working fine. but page load call this code on each event call. So is it good things to call and fatch record from database for creating table/grid? again and again for same page?
-
write this code in protected void Page_PreInit(object sender, EventArgs e) { LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not } private void ctrl_Click(object sender, EventArgs e) { } dont write in protected void Page_Load(object sender, EventArgs e) { }
Hi Visnu, Thanks for helping me. It does not change the behaviour of link button. Page_PreInit(object sender, EventArgs e) calls the functions to creats the button programatically but when i click the link button then the dynamic table gets disapeared. for the solution of problem i wrote the code in the Page_load event and got the results. But my question, Is it good thing to call same function again and again and get data from database to create table dynamic for same page?
-
thanks for Helping me. i pasted the code in Page load and it is working fine. but page load call this code on each event call. So is it good things to call and fatch record from database for creating table/grid? again and again for same page?
for Dynamically created controls its good.its for avoid disappearing control in PlaceHolder after postback.
Raju.M