3

Was hoping someone would be able to help me. I'm basically attempting to call a method within an class using a HTML button. The reasoning behind this is because I'm intending to make it work similar to a login (and wanting it to hold sessions upon logging in), as well as keeping the current, visual effects of the button.

Upon research, there is some places basically saying you can do it, however it simply won't work for me for some reason. Where it says you can do it- HTML Button like a ASP.NET Button

Here is my code-

<form runat="server">
    <div class="input">
        <asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
        <span><i class="fa fa-envelope-o"></i></span>
    </div>

    <div class="input">
        <asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
        <span><i class="fa fa-lock"></i></span>
    </div>
</form>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>

With the C# code containing

    public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void doIt(object sender, EventArgs e)
    {
        Response.Redirect("index.html");
    }
}

Am I going about this the totally wrong way? (The redirect was just there as a test for now, until I'm able to get it to run the method since it doesn't run it.)

Any help is appreciated.

Below is all my HTML code:

<form class="login">
    <fieldset>
        <legend class="legend">Login</legend>
        <form runat="server">
            <div class="input">
                <asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
                <span><i class="fa fa-envelope-o"></i></span>
            </div>

            <div class="input">
                <asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
                <span><i class="fa fa-lock"></i></span>
            </div>
            <button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
        </form>

    </fieldset>

    <div class="feedback">
        login successful <br />
        redirecting...
    </div>
</form>
7
  • 3
    Put the button inside the form. Commented Jan 26, 2016 at 15:43
  • Method still doesn't run when inside form tag. Commented Jan 26, 2016 at 15:49
  • Then you have something else going on because your code worked just fine for me with that change. Do you have any jQuery or JavaScript involved? Commented Jan 26, 2016 at 15:52
  • Added all html code. (took from a website for design but still editing. I noticed "modernizr.js" script. Removed it, but never fixed it. Commented Jan 26, 2016 at 15:58
  • Do you have any javascript errors showing on the page? Commented Jan 26, 2016 at 15:59

1 Answer 1

4

You have a nested form in your HTML, which is invalid markup. Remove the inner form, and add runat="server" to the form remaining. Then place your button inside the form like so:

<form class="login" runat="server">
    <fieldset>
        <legend class="legend">Login</legend>
        <div class="input">
            <asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
            <span><i class="fa fa-envelope-o"></i></span>
        </div>

        <div class="input">
            <asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
            <span><i class="fa fa-lock"></i></span>
        </div>
        <%-- place the button here --%>
        <button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
    </fieldset>

    <div class="feedback">
        login successful
        <br />
        redirecting...
    </div>
</form>

Each web form - .aspx - should contain one form, and must contain runat="server" for the .cs file to manage. All server-side elements - elements with the runat="server", such as your <button> must be contained within it for the code behind to be able to point to it. If you don't, you should receive a Invalid postback or callback argument exception yellow screen of death.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I appreciate the help (Spent hours trying to figure out what I was doing wrong!!!). Worked brilliantly!

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.