Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hey there ,
After doing many searches in google I came to here hoping to find the answer
I want to change the value of onClick="to new value" in code behind
for example:
if in the source onClick="Button1_click"
I want to change it from the code behind to
onClick="btnclick"

I'm making a light game in my web-project and now stuck with this
thanks in advance.
Posted

Detach the existing event handler:
button.Click -= Button1_click;
then assign the new event handler:
button.Click += btnclick;

If this is not working that means you are assigning the event handler in your markup. Remove that and do the assignment in code-behind maybe with a (IsPostBack) check, and then put the above piece of code. Try this and let me know.
 
Share this answer
 
v3
Comments
weso_f 14-Jun-11 4:49am    
thanks for reply
but I'm trying and it's not changing ! here is my code

public partial class _Default : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Click -= Button1_Click;
Button1.Click += btnclick;
}
protected void btnclick(object sender, EventArgs e)
{
Label1.Text = "worked";
}
}
weso_f 14-Jun-11 4:55am    
I removed the attribute from the source and assigned it in page load it's still the same no action occurs here is my nwe code:

public partial class _Default : System.Web.UI.Page
{

public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Button1.Click += Button1_Click;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Click -= Button1_Click;
Button1.Click += btnclick;
}
protected void btnclick(object sender, EventArgs e)
{
Label1.Text = "ll";
}
}
and here is it's source :
<code>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
     
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" /></div>
</form>
</body>
</html>


RakeshMeena 14-Jun-11 5:04am    
If you look carefully your button's click event is bound to "Button1_Click1" in the source. Remove that and try!
weso_f 15-Jun-11 18:01pm    
I found alternative and easier way that has the same aim to make my game
it's by changing the button text !
thanks alot
Try to this

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click -= Button1_Click;
Button1.Click += btnclick;
}
protected void btnclick(object sender, EventArgs e)
{
Label1.Text = "worked";
}
}
 
Share this answer
 
Comments
weso_f 14-Jun-11 5:03am    
but I need to change the value in the (Button1_Click) procedure not in the page_Load

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900