如何:覆盖所有Html.TextBox - ASCX不叫!为什么? [英] How To: Overwriting all Html.TextBox - ascx not called! Why?

查看:211
本文介绍了如何:覆盖所有Html.TextBox - ASCX不叫!为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加到所有的文本框在所有的aspx网站,如:

How can I add to ALL textboxes in ALL aspx sites, like:

<%: Html.TextBoxFor(model => model.Firstname, new { style = "float: left; width: 4.1em;", maxlength = "4" })%>

而无需搜索它们,手动添加类似的JavaScript函数:

a javascript function without searching them and adding manually something like:

new { onkeyup='Foo(this); return false;' }

想法:写一个 ASCX 类似下面的一个!

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%: Html.TextBox(   string.Empty, 
                    ViewData.TemplateInfo.FormattedModelValue, 
                    new { onkeyup = "foo(this); return false;" })%>

请让我们说这个 ASCX 被命名为 Test.ascx 结果
并且它存储在 \\浏览\\共享\\ EditorTemplates \\

Please let us say this ascx is named Test.ascx
and that it is stored in \Views\Shared\EditorTemplates\.

我的问题是,所谓的Test.ascx心不是。结果
为什么呢?结果
必须参照被添加到所有的aspx网站?结果
是否有一个&LT; System.String&GT; 在继承失踪

My problem is that "Test.ascx" isnt called.
Why?
Must a reference be added to all aspx sites?
Is there a < System.String> in the Inherit missing?

任何帮助就可以了。

非常感谢你在前进。

推荐答案

为了您的自定义编辑器的模板被称为你需要使用:

In order for your custom editor template to be called you need to use:

<%= Html.EditorFor(model => model.Firstname) %>

而不是:

<%= Html.TextBoxFor(model => model.Firstname, new { style = "float: left; width: 4.1em;", maxlength = "4" })%>

通过使用Html.TextBoxFor你明确地硬编码此值,并要一个&LT;输入类型=文本... ,你不能覆盖或替换它。这就是为什么它是很好的做法,使用 Html.EditorFor ,因为它允许你修改这是通过定义一个自定义模板呈现方式的原因视图\\共享\\ EditorTemplates 文件夹中。

By using Html.TextBoxFor you are explicitly hardcoding this value and that you want an <input type="text" ... and you cannot override or replace it. That's the reason why it is good practice to use Html.EditorFor as it allows you to modify the way this is rendered by defining a custom template in the Views\Shared\EditorTemplates folder.

现在,如果你编辑模板名为: Test.ascx 你可以调用它是这样的:

Now if you editor template is called: Test.ascx you could invoke it like this:

<%= Html.EditorFor(model => model.Firstname, "Test") %>

或与<一个装饰您的视图模型属性href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx\"相对=nofollow> [UIHint] 属性:

[UIHint("Test")]
public string Firstname { get; set; }

,然后简单:

<%= Html.EditorFor(model => model.Firstname) %>

这篇关于如何:覆盖所有Html.TextBox - ASCX不叫!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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