0

I am working on a fancy design for a ASPX page, but I am having problems firing the buttons to run code. Here's what I have:

This is the ASPX code:

<button runat="server" id="btnLogin" onserverclick="LoginClick_Click"></button>

And the codebehind:

protected void LoginClick_Click(object sender, EventArgs e)
    {
        notice.Visible = true;


   //     Response.Redirect("");

    }

I am clicking the button to see if anything shows up (even to a Blank broken page [yes I uncommented it]) but no go. I believe this is something simple but I've been struggling here for nearly an hour and no go. Joys of coding I guess.

Edit:

 <button runat="server" id="btnLogin" OnServerClick="btnLogin_Click" ></button>

That worked. Capitalization it seems.

Lord Relix
  • 922
  • 5
  • 23
  • 50

2 Answers2

1

You need to be more careful with your syntax.

<asp:Button runat="server" id="btnLogin" OnClick="btnLogin_Click" />

Code behind

protected void btnLogin_Click(object sender, EventArgs e)
    {
        notice.Visible = true;
    }
mason
  • 31,774
  • 10
  • 77
  • 121
  • I am not using ASP's built in buttons. Its a straight HTML button. – Lord Relix Jan 07 '14 at 20:23
  • Unless... am I supposed to input – Lord Relix Jan 07 '14 at 20:24
  • You have indicated this question deals with ASP.NET Web Forms. Therefore, you should use the controls provided. The asp:Button is specifically made for calling a function on the server side. Unless you have a specific reason for not using ASP.NET controls, I suggest you use them. – mason Jan 07 '14 at 20:30
  • I can use an ImageButton (its an Image) but I lose CSS/JQuery effects I want to have. – Lord Relix Jan 07 '14 at 20:39
  • @LordRelix You can apply CSS and jQuery effects to an ASP.NET button or ImageButton. You should not let that stop you from using the correct control. – mason Jan 07 '14 at 20:41
  • Really? Didn't know that. I thought they were mostly static controls. Will look into that, didn't know it was possible. – Lord Relix Jan 07 '14 at 20:42
  • While I agree that WebControls (instead of HtmlControls) should be used where possible, there is nothing 'wrong' with using plain vanilla HTML controls in a WebForms scenario, which is why controls in the System.Web.UI.HtmlControls namespace exists; in some scenarios (legacy apps etc) it may not be possible to upgrade everything to WebControls. To the OP - to eliminate the possibility of capitalization problems, wire up your events in Page Init instead; that way there's no possibility of capitalization problems, because the handler declaration is not in the markup. – sh1rts Jan 08 '14 at 02:40
0

If you're really interested in using a different button from an ASP control, you might want to just poke a hidden ASP button with the desired functionality using some inline javascript and jquery...

<asp:Button ID="btnLogin" runat="server" Visible="False" />
<button type="button" id="btnFancy" onclick="serverClick();">TextHere</button>

--

<script type="text/javascript">
function serverClick() {
    $('#<%=btnLogin.ClientID%>').click();
    }
</script>

I wouldn't suggest this, but it should work for you.

cmann83
  • 76
  • 1
  • 6