[英]How do I set a click event in C#?
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. 我是C#的新手(我的工作是让我从JavaScript转换),由于某种原因,我找不到设置调用方法的按钮的简单示例。
I am using C# ASP.NET MVC 2 with the ASPX view engine. 我将C#ASP.NET MVC 2与ASPX视图引擎一起使用。 This is not ASP.NET Web Forms .
这不是ASP.NET Web窗体 。
My Index.aspx looks like: 我的Index.aspx看起来像:
<%@ 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" />
编辑:好的,这样做的按钮就像:
<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()
? 告诉我博客是未定义的...我怎么称呼
blogging()
?
If you meaning to call an action method from View
then you might try to use one of the following examples below. 如果要从
View
调用动作方法,则可以尝试使用以下示例之一。 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. 在
ASP.NET MVC
创建到控制器动作的链接时,最好使用通用的ActionLink
方法,因为它允许重构友好的强类型链接。
Default: ActionLink: 默认值: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: 您可能会认为可以像这样结合使用
ActionLink
和Image和Button
帮助器:
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... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.