如何在C#中设置点击事件? [英] How do I set a click event in C#?

查看:72
本文介绍了如何在C#中设置点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手(我的工作是让我从JavaScript转换),由于某种原因,我找不到设置调用方法的按钮的简单示例.

我将C#ASP.NET MVC 2与ASPX视图引擎一起使用.这不是不是ASP.NET Web表单.

我的Index.aspx看起来像:

 <%@页面标题="语言="C#" MasterPageFile =〜/Views/Shared/Site.Master" Inherits ="System.Web.Mvc.ViewPage< dynamic>"%>< asp:Content ID ="Content1" ContentPlaceHolderID ="TitleContent" runat ="server">网志</asp:Content>< asp:Content ID ="Content2" ContentPlaceHolderID ="MainContent" runat ="server">< h2>博客</h2>< button ID ="btnBlog" onclick ="blogging" runat ="server"> Blog</button></asp:Content> 

我已经尝试了几种方法来做到这一点;最后一个是:

 公共事件EventHandler blogging(){System.Diagnostics.Debug.Write("clicked");} 

好的,这样做按钮:< asp:Button ID ="btnBlog" OnClick ="blogging" runat ="server"/>

和方法:

 受保护的无效博客(对象发送者,EventArgs e){System.Diagnostics.Debug.Write("clicked");} 

告诉我博客是未定义的...我怎么称呼 blogging()?

解决方案

如果要从 View 调用动作方法,则可以尝试使用以下示例之一.在 ASP.NET MVC 中创建指向控制器动作的链接时,最好使用通用的 ActionLink 方法,因为它允许重构友好的强类型链接.

默认:ActionLink:

  @ Html.ActionLink(删除",删除",新的{id = item.ID}) 


但是,如果我们想要链接到动作的图像怎么办?您可能会认为可以像这样组合 ActionLink 和Image和 Button 助手:

使用按钮:

 < button onclick ="location.href ='@ Url.Action(" Index," Users)';;返回false;>取消</button> 

(带有参数)

 < button onclick ="location.href ='@ Url.Action(" Detail," Admin,new {Model.ProductID})';返回false;> Detail</button> 

 <输入类型=按钮" title =删除" value =删除"onclick ="location.href ='@ Url.Action(" Delete," movies,new {id = item.ID})'"/> 


使用图片:

 < a href ="@ Url.Action(" Delete," movies,new {id = item.ID})" title =编辑">< img src ="../../Content/Images/Delete.png"/></a> 

希望这对您有帮助...

I am new to C# (my job is making me convert from JavaScript) and for some reason I cannot find a straightforward example of setting up a button that calls a method.

I am using C# ASP.NET MVC 2 with the ASPX view engine. This is not ASP.NET Web Forms.

My Index.aspx looks like:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Blogs
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Blogs</h2>
    <button ID="btnBlog" onclick="blogging" runat="server">Blog</button>

</asp:Content>

and I have tried several ways of doing this; this last one being:

public event EventHandler blogging()
{
    System.Diagnostics.Debug.Write("clicked");

}

Edit: Ok so doing the button like: <asp:Button ID="btnBlog" OnClick="blogging" runat="server" />

and method:

protected void blogging(object sender, EventArgs e)
{
    System.Diagnostics.Debug.Write("clicked");

}

Tells me that blogging is undefined... how do I call blogging()?

解决方案

If you meaning to call an action method from View then you might try to use one of the following examples below. When creating a link to a controller action in ASP.NET MVC, using the generic ActionLink method is preferable, because it allows for strongly typed links that are refactoring friendly.

Default: ActionLink:

@Html.ActionLink("Delete", "Delete", new { id = item.ID }) 


However, what if we want to have an image that links to an action? You might think that you could combine the ActionLink and Image and Button helpers like this:

Using Button:

<button onclick="location.href='@Url.Action("Index", "Users")';
    return false;">Cancel</button>

(with parameters)

<button onclick="location.href='@Url.Action("Detail", "Admin",
    new { Model.ProductID })';return false;">Detail</button>

or

<input type="button" title="Delete" value="Delete" 
    onclick="location.href='@Url.Action("Delete", "movies", new { id = item.ID })'" />


Using Image:

<a href="@Url.Action("Delete", "movies", new { id = item.ID })" title="Edit">
    <img src="../../Content/Images/Delete.png" />
</a>

Hope this helps...

这篇关于如何在C#中设置点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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