Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在ASP.NET MVC 3中使用System.Web.UI.WebControl.Button_Asp.net Mvc 3_Custom Controls_Imagebutton - Fatal编程技术网

Asp.net mvc 3 如何在ASP.NET MVC 3中使用System.Web.UI.WebControl.Button

Asp.net mvc 3 如何在ASP.NET MVC 3中使用System.Web.UI.WebControl.Button,asp.net-mvc-3,custom-controls,imagebutton,Asp.net Mvc 3,Custom Controls,Imagebutton,我想在ASP.NET MVC 3中构建自定义控件。我想创建一个按钮,我可以传递不同的参数,如CSS类名等。然而,我希望能够跟踪它的事件,如onClick或onHover 我怎么做?求你了,我受够了 谢谢。ASP.NET MVC中不再存在服务器端控件和事件。如果您想跟踪诸如oncilck和onhover之类的事件,您可以使用javascript。你也可以使用HTML助手。只需忘记ASP.NET MVC应用程序中的System.Web.UI名称空间即可。您可以创建类似以下的HtmlHelper: p

我想在ASP.NET MVC 3中构建自定义控件。我想创建一个按钮,我可以传递不同的参数,如CSS类名等。然而,我希望能够跟踪它的事件,如onClick或onHover

我怎么做?求你了,我受够了


谢谢。

ASP.NET MVC中不再存在服务器端控件和事件。如果您想跟踪诸如
oncilck
onhover
之类的事件,您可以使用javascript。你也可以使用HTML助手。只需忘记ASP.NET MVC应用程序中的
System.Web.UI
名称空间即可。

您可以创建类似以下的HtmlHelper:

public static class Helpers {
    public static MvcHtmlString Button(this HtmlHelper html, string id, string url) {
        var builder = new TagBuilder("a");

        builder.MergeAttribute("href", url);
        builder.MergeAttribute("id", id);
        builder.AddCssClass("custom-button");

        return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
    }
}
他认为:

@Html.Button("CustomButton", "http://localhost")
使用JQuery的JS:

$(function() {
    $(".custom-button").click(function() {
        // This will handle all the click events of the buttons
    });

    $("#CustomButton").click(function() {
        // This will handle the click event on the specific button
    });
});
正如Darin所说,asp.NETWebForms中不再有用户控件,我认为这是一件非常好的事情

因此,上面的代码是构建“用户控件”的一种方法