0

I'm making a ASP.NET Web Forms application. I am trying to have a simple way where I can access several controls on the form.

I am trying to access html buttons' serverClick event but it has not been working and only worked on a single button per page.

I first tried to use this code to get the click event when the HTML button was clicked. This is the code for the HTML button.

<form runat="server">
<div class="btn-container">
<a runat="server" onserverclick="infobutton_Click" class="inf-btn" exc-content="inf_button" id="infoButton">Click Here for More Information</a>
</div>
</form>

This code gets the button's click event and displays a sample message box.

Protected Sub infoButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('" & "The Button Was Clicked" & "')", True) 'Displays a JavaScript Alert
End Sub

This code works fine for one button per page, however, I want to use multiple buttons on one page. If I remove the <form> tag, the button click event does not register. If I add a <form> tag for each and every button, it reports an error that there cannot be multiple <form> tags. If I enclose the whole page with a <form> tag, the error reports that the configuration is "corrupted".

This would be straightforward if I had a ASP.NET button control (<asp:button>), but I cannot convert all elements on the page.

I'm not sure what is wrong as all information online says it is correct. Is there any way I can fix this?

6
  • You say you want to use multiple buttons on one page. Can you show us what you've tried? It's unclear, to me anyway, why that didn't work. Forget about the form tags, just add another button or two and show us what you did on the page and in code-behind. Repeating what you did with the first button should be all you need to do. Commented May 21, 2021 at 0:59
  • @wazz I just pasted the same <a> button tag just under a different ID & onserverclick name. I cant put both in a form, since there are other elements between them. This only works when theres a form tag enveloping the tag Commented May 21, 2021 at 1:26
  • Yes a (1) form tag is required, I didn't mean don't use a form tag. But I still don't understand what the problem is. Commented May 21, 2021 at 1:28
  • I have two buttons. There are elements between them. If I envelop those elements along with the two button tags, ASP reports a file corruption error. Commented May 21, 2021 at 1:29
  • @wazz for example, body tags cannot be in form. Commented May 21, 2021 at 1:32

1 Answer 1

1
<body>
    <form id="form1" runat="server">

        <div class="btn-container">
            <a runat="server" onserverclick="infobutton1_Click" id="infoButton1">
                Click Here for More Information</a>
            <a runat="server" onserverclick="infobutton2_Click" id="infoButton2">
                Click Here for More Information</a>
            <a runat="server" onserverclick="infobutton3_Click" id="infoButton3">
                Click Here for More Information</a>
        </div>

    </form>
</body>

Code Behind (C#)

protected void infobutton1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 1 Was Clicked" + "')", true);
}
protected void infobutton2_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 2 Was Clicked" + "')", true);
}
protected void infobutton3_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 3 Was Clicked" + "')", true);
}
Sign up to request clarification or add additional context in comments.

4 Comments

If I envelop both buttons in a form, I get this error: Server Error in '/' Application. The state information is invalid for this page and might be corrupted. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.
That's not really related to the code. That's more to do with (something like) the browser's cache disagreeing with new stuff you're trying to load. This can usually go away by reloading the page with F5 or Ctrl+F5, or clearing the cache or more extreme things like restarting the browser and/or IDE, and/or clearing cached ASP.NET folders (should not be necessary). Make sure there are no remnants of old HTML or random things on the page that should be deleted.
I believe I found out the error, sorry. There was a random <form> tag lying around that was hidden from the search. It works now.
Cool. That's usually what happens.

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.