![](/img/trans.png)
[英]C# How do I create code to set a form back to default properties, with a button click event?
[英]How do I set a click event in C#?
我是C#的新手(我的工作是让我从JavaScript转换),由于某种原因,我找不到设置调用方法的按钮的简单示例。
我将C#ASP.NET MVC 2与ASPX视图引擎一起使用。 这不是ASP.NET Web窗体 。
我的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>
我尝试了几种方法来做到这一点; 最后一个是:
public event EventHandler blogging()
{
System.Diagnostics.Debug.Write("clicked");
}
编辑:好的,这样做的按钮就像: <asp:Button ID="btnBlog" OnClick="blogging" runat="server" />
和方法:
protected void blogging(object sender, EventArgs e)
{
System.Diagnostics.Debug.Write("clicked");
}
告诉我博客是未定义的...我怎么称呼blogging()
?
如果要从View
调用动作方法,则可以尝试使用以下示例之一。 在ASP.NET MVC
创建到控制器动作的链接时,最好使用通用的ActionLink
方法,因为它允许重构友好的强类型链接。
默认值:ActionLink:
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
但是,如果我们想要链接到动作的图像怎么办? 您可能会认为可以像这样结合使用ActionLink
和Image和Button
帮助器:
使用按钮:
<button onclick="location.href='@Url.Action("Index", "Users")';
return false;">Cancel</button>
(带参数)
<button onclick="location.href='@Url.Action("Detail", "Admin",
new { Model.ProductID })';return false;">Detail</button>
要么
<input type="button" title="Delete" value="Delete"
onclick="location.href='@Url.Action("Delete", "movies", new { id = item.ID })'" />
使用图片:
<a href="@Url.Action("Delete", "movies", new { id = item.ID })" title="Edit">
<img src="../../Content/Images/Delete.png" />
</a>
希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.