ASP.NET的Button Click Event背后的C#代码 [英] C# code behind for Button Click Event, ASP.NET

查看:296
本文介绍了ASP.NET的Button Click Event背后的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用同事在.aspx中提供的HTML,并且我需要在.aspx.cs(C#)中编程他使用HTML创建的按钮的功能. 我正在处理的代码如下:

I'm working with HTML provided by coworker in .aspx and I need to program in .aspx.cs(C#) the functionality of the button he created using HTML. The code I'm dealing with is as follows:

<div>
    <button style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue">Ship</button>
    <button style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue">Rate</button>
</div>

我是HTML的新手,但是在寻找类似问题的解决方案时,我经常看到功能按钮的HTML代码看起来更像

I'm new to HTML but in looking at solutions to similar questions I often see the HTML code for a functional button looking more like

<button type="submit" runat="server" id="btnLogin" class="button" onclick="btnLogin_Click();">

有时带有

<asp:

开头.我已经尝试过简单地将runat和onclick字段添加到HTML中,这些字段与后面的aspx.cs代码中的C#方法相对应,但是我无法使按钮单击实际上触发了后面代码中的方法.我以为,如果我将在onclick之后加上名称的方法添加到C#aspx.cs代码中,则只需单击按钮即可执行此操作,但我显然弄错了.

at the beginning. I've tried simply adding the runat and onclick fields to the HTML that correspond with a C# method in the aspx.cs code behind but I haven't been able to make the button click actually trigger the method in the code behind. I've assumed that if I add the method with the name that follows onclick to the C# aspx.cs code then it would simply execute this upon the clicking of the button but I'm clearly mistaken.

我可以在不更改提供给我的HTML的情况下,在后面的代码中对按钮的功能进行编程吗?还是我必须先更改HTML?如果是,该如何更改?

Can I program the button's functionality in the code behind without changing the HTML provided to me? Or must I alter the HTML first and if so how?

推荐答案

您可能不愿意在按钮上添加runat="server"属性,但坦率地说,我从未尝试过,最好使用适当的Web控件要走的路.

You could probably get away with adding the runat="server" attribute to your buttons but I've never tried it and frankly, using proper web controls would be the best way to go.

替换它们根本不会改变页面的外观.只需添加CssClass="xxx"即可应用css(如果有的话),否则它们仍将呈现为标准html元素.

Replacing them shouldn't change the look and feel of the page at all. Just add CssClass="xxx" to apply the css if there is any, otherwise they render to standard html elements anyway.

您的标记应如下所示:

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

代码中匹配的事件处理程序如下所示:

The matching event handler in your code would look like this:

protected void btnLogin_Click(object sender, EventArgs e)
{
    // Here's where you do stuff.
}

这篇关于ASP.NET的Button Click Event背后的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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