简体   繁体   中英

Why are my HTML buttons in Visual Studio not creating Event Handlers when double clicked?

I have added buttons into a web form in Visual Studio 2015 using HTML. So those buttons are in the design tab. When i double click the button in the Design Tab. It is not opening the ASPX.CS page. Is that normal? I was under the impression that i would work like that. Instead i have to go and manually code each event handler.

I have searched google and tried to run repairs on Visual Studio but found no avail.

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="Module2LabExercise.WebForm1" %>

<!DOCTYPE html> 
<html>  
    <head>    
    <title> 
        Currency Converter
    </title>  
</head>  
<body>    
    <form runat = "server">      
        <div>        
            Convert: &nbsp;        
            <input type = "text" ID = "US" runat = "server" />        
            &nbsp; U.S. dollars to &nbsp;        
            <select ID = "Currency" runat = "server" />        
            <br /> <br />       
            <input type = "submit" value = "OK" ID = "Convert" 
OnServerClick = "Convert_ServerClick" runat = "server" />  
            <input type = "submit" value = "Show Graph" ID = "ShowGraph" 
OnServerClick = "ShowGraph_ServerClick" runat = "server" />      
            <br /> <br />          
            <img id="Graph" runat="server" src="//:0"/>
            <br /> <br />        
            <p style = "font-weight: bold" ID = "Result" runat = "server" 
>
            </p>  
        </div>    
   </form>  
    </body> 
</html>

`

I was thinking that if i double click the button. It would auto open the Event Handler and create the event handler. If i am wrong. Please correct me and let me know. Just coming here because im out of ideas.

you add that code in your backend of button implementation: here is code

Response.redirect("ex.aspx");

ex.aspx is your file name

if it not working then add:

Server.Transfer("ex.aspx");

VS won't do this for non-ASP tags, but if you instead put:

<asp:Button ID="Convert"  runat="server" />

instead of

<input type = "submit" ID = "Convert" runat = "server" />  

Then you'll get that feature

As for

It is not opening the ASPX.CS page. Is that normal?

yes it is normal as the button is an HTML input[type="submit"] with runat="server and not an asp.net wrapped button like: <asp:button> . Hence it is not transpiled by the Asp.net .

And why are you using runat="server" on an HTML button? If you really want a server-side event to be called use the <asp:Button> that way you will be able to create the click-event on double-clicking the button.

But if you still persist on using it you have to 3 ways attach the click event-handler to the HTML button.

Event handler in aspx designer page.

//attach a script tag to your aspx page and add the click event to it.

<script language="C#" runat="server"> 
    protected void Convert_ServerClick_manual1(object sender, EventArgs e)
    {
        //your code here
    }
</script>

Now just add an onserverclick="Convert_ServerClick_manual1" attrubute to the button.

Attach an event handler on the pageload event of aspx.cs page

protected void Page_Load(object sender, EventArgs e)
{
    Convert.Click += Convert_ServerClick_manual2;
}

now define the event handler below the pageload event:

private void Convert_ServerClick_manual2(object sender, EventArgs e)
{
    //your code here
}

Manual event handler

//just create the event handler for your button on the `aspx.cs` page and paste the handler name to the `HTML` button.

private void Convert_ServerClick_Manual3(object sender, EventArgs e)
{
    //your code here
}

Then in your design page use:

<input type = "submit" value = "OK" ID = "Convert" OnServerClick = "Convert_ServerClick_manual3" runat = "server" /> 

manually create an event handler for the input button[runat="server"] in the <yourPage>aspx.cs and attach it to the HTML button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM