为什么我在 Visual Studio 中的 HTML 按钮在双击时没有创建事件处理程序? [英] Why are my HTML buttons in Visual Studio not creating Event Handlers when double clicked?

查看:36
本文介绍了为什么我在 Visual Studio 中的 HTML 按钮在双击时没有创建事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 HTML 将按钮添加到 Visual Studio 2015 中的 Web 表单中.所以这些按钮在设计选项卡中.当我双击设计"选项卡中的按钮时.它没有打开 ASPX.CS 页面.这是正常的吗?我的印象是我会那样工作.相反,我必须去手动编码每个事件处理程序.

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.

我在谷歌上搜索过并尝试在 Visual Studio 上进行修复,但没有找到.

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>

</表单></html>

`

`

我在想,如果我双击按钮.它将自动打开事件处理程序并创建事件处理程序.如果我错了.请纠正我并让我知道.来这里只是因为我没有想法.

解决方案

推荐答案

至于

它没有打开 ASPX.CS 页面.正常吗?

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.

是的,这是正常的,因为按钮是带有 runat="serverHTML input[type="submit"] 而不是asp.net 包裹按钮,如:.因此它不会被 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.

你为什么在 HTML 按钮上使用 runat="server" ?如果您真的希望调用服务器端事件,请使用 <asp:Button> 这样您就可以在双击时创建 click-event按钮.

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

但是如果您仍然坚持使用它,您必须通过 3 种方式将 click 事件处理程序附加到 HTML 按钮.

Event handler in aspx designer page.

aspx 设计器页面中的事件处理程序.

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

现在只需为按钮添加一个 onserverclick="Convert_ServerClick_manual1" 属性.

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

aspx.cs页面的pageload事件上附加一个事件处理程序

now define the event handler below the pageload event:

现在在 pageload 事件下面定义事件处理程序:

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" /> 

aspx.cs 中的输入 button[runat="server"] 手动创建一个 event handler 和将其附加到 HTML 按钮.

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

这篇关于为什么我在 Visual Studio 中的 HTML 按钮在双击时没有创建事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆