如何添加的事件处理程序的按钮事件在C#中从源视图(CSS) [英] How to add an event handler for events from button in C# from source view (aspx)

查看:112
本文介绍了如何添加的事件处理程序的按钮事件在C#中从源视图(CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用于创建代码隐藏(web表单)事件处理假定从HTML源视图按钮,最简单的方法?

What is the easiest way to create code-behind (webforms) event handlers for say a button from HTML source view?

在VB.NET还是比较方便的切换到代码页的后面,使用对象和事件的组合框沿顶来选择和创造。

In VB.NET it is quite easy to switch to code behind page and use the object and events combo boxes along the top to select and create.

在C#中的缺失(我真的不喜欢设计视图)。

In c# those are missing (and I really don't like design view).

推荐答案

我同意这是用VB C#则没有价值的。我个人的喜好是简单地增加一个功能具有以下签名(始终有效):

I agree that this is less trivial with C# then with VB. My personal preference is to simply add a function with the following signature (always works):

protected void MyButtonName_Clicked(object sender, EventArgs e)
{
    Button btn = (Button) sender;  // remember, never null, and cast always works
    ... etc
}

然后,HTML / ASP.NET部分的代码视图中(又名声明代码的),你只需添加:

Then, inside the code view of the HTML/ASP.NET part (aka the declarative code) you simply add:

<asp:Button runat="server" OnClick="MyButtonName_Clicked" />



我觉得这个速度在实践中再经过几个属性菜单,这并不总是取决于工作会焦点和成功编译等,您可以调整 EventArgs的来不管它是针对该事件,但的所有的事件与上面的基本签名工作。如果你不知道类型,只需将在该行断点,将鼠标悬停在电子对象时,它打破找出实际类型(但大部分的时间你就会知道这一点,事前)。

I find this faster in practice then going through the several properties menus, which don't always work depending on focus and successful compile etc. You can adjust the EventArgs to whatever it is for that event, but all events work with the basic signature above. If you don't know the type, just place breakpoint on that line and hover over the e object when it breaks to find out the actual type (but most of the time you'll know it beforehand).

几次这样做了以后,它成为第二天性。如果你不喜欢它,等待VS2010,它已经变得更加容易了一下

After a few times doing this, it becomes a second nature. If you don't like it, wait a moment for VS2010, it's been made much easier.

注:VB和C#从来没有表现出物体或事件那放在命名容器(即GridView控件,ListView控件)的元素。在这种情况下,你必须做这样的看法。

这篇关于如何添加的事件处理程序的按钮事件在C#中从源视图(CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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