1

I am rendering some html in code behind using StringBuilder which includes a button. I am also trying to add a handler to this and I think perhaps not surprisingly it does not fire. Can I include an event handler to a button in this way?

sb.Append("<td>");
sb.Append("<input type='button' runat='server' id='butResetPassword' value='Reset Password' onserverclick='butSendEmail_Click' />");
sb.Append("</td>");
User.InnerHtml = sb.ToString();

this is the eventhandler that appears in the same code behind page

protected void butSendEmail_Click(object sender, EventArgs e)
{
    labTester.InnerText = "Thanks for clicking me";

 }
1
  • You are rendering <input type='button' runat='server' id='butResetPassword' value='Reset Password' onserverclick='butSendEmail_Click' /> on client side, this doesn't make sense. onserverclick on client side will not do the job for you. Commented Jun 18, 2013 at 10:45

1 Answer 1

1

I answered a similar question of yours recently.

Unless you have a really good reason to generate markup in the code behind, keep it separate, otherwise stick to classic ASP.

You can do:

Markup:

<asp:Button runat="server" id="butResetPassword" OnClick="butSendEmail_Click" />

Code behind:

protected void butSendEmail_Click(object sender, EventArgs e)
{
    //Reset the password here
    labTester.InnerText = "Thanks for clicking me";
}

Edit* If all you need to do is display that confirmation message and not interact with any data or controls, then you could bind the button to a js function, change onserverclick to onclick:

<input type='button' runat='server' id='butResetPassword' value='Reset Password' onclick='butSendEmail_Click' />

And then have some js to handle this:

<script type="text/javascript">
   function butSendEmail_Click() {
       alert("Thanks for clicking me");

       //Or set the labels text
       var elem = document.getElementById("labTester");
       elem.InnerHtml = "Thanks for clicking me";
   }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Will avoid that. Thanks. Will use the DataList instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.