MVC、ascx 和 Javascript 最佳实践 - 如何创建自包含控件? NerdDinner 做错了

发布于 2024-09-19 03:55:17 字数 604 浏览 1 评论 0原文

问题是如何在 ASP.Net MVC2 中创建控件/部分视图/编辑模板(ascx 文件),以便它们“自包含”。自包含在这里意味着如果某些 Javascript 代码应应用于控件,则该代码不包含在页面 (aspx) 中,而是包含在控件本身中。 具体例子: NerdDinner 的 DateTime.ascx。该文件包含 JS 代码,使文本框成为一个不错的日期时间选择器。

我的问题: 包含页面有 jQuery.js、timepicker.js、jQueryUI 的 css 和 datepicker 的 css。 (对于 NerdDinner,这些都在母版页中)。因此,每当我想对 DateTime 类型使用精美的 DateTimePicker 时,包含的页面必须了解这些依赖项,并且必须添加所有 js 和 css 文件。我认为我在这里想念的是一个替代 ClientScript.RegisterClientScriptBlock 的解决方案。

同一 NerdDinner 示例的其他问题: 在 DateTime.ascx 中,它显示 $('#Dinner_EventDate') 这既依赖于容器类型又依赖于属性名称。这不是 DateTime 共享 EditorTemplate 的通用解决方案。

有什么建议吗?

The question is how to create controls/partial views/edittemplates (ascx files) in ASP.Net MVC2 so that they are "self-containing". Self-containing means here that if some Javascript code should be applied on the control, that's not included in the page (aspx), but in the control itself.
Specific example:
NerdDinner's DateTime.ascx. The file contains the JS code which makes the textbox a nice DateTime picker.

My problems:
The containing page has the jQuery.js, the timepicker.js, the jQueryUI's css and the datepicker's css. (In case of NerdDinner these all are in the master page). So whenever I'd like to use the fancy DateTimePicker for my DateTime types, the containing page has to be aware of these dependencies and has to add all the js and css files. I think what I miss here is a solution which replaces the ClientScript.RegisterClientScriptBlock.

Other problem with the same NerdDinner example:
In the DateTime.ascx it says $('#Dinner_EventDate') which is a dependency both on the container type and on the property name. That's not a general solution for a DateTime shared EditorTemplate.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹短裙飘 2024-09-26 03:55:17

您需要从页面链接到这些文件。否则就会破坏这些元素包含在 html 中的方式以及控件包含在页面中的位置。

我们所做的就是使用一些额外的代码(完成一次)将所有这些单独的 js/css 文件连接到一个文件中。这样您就只能从母版页链接到单个文件。只要里面没有特别重的东西就很合理。

对于id问题,将jquery改为按css class搜索


原始答案:忽略下面,我认为它还没有在单独的文件中。

将其移至 .js 文件,并从使用 id 更改为使用类。

如果您想保持其结构化/仅专用于该控件代码的文件中的 .js 代码,您可以这样做并添加一些额外的代码,将站点中的所有各种 .js 文件连接到单个 .js 文件。这使您可以根据需要构建它,而不会收到不必要的额外请求。

You need to link to these files from the page. Doing otherwise will be hacking your way against the way these elements are included in html vs. where your control is included in the page.

What we do is we join all these separate js/css files into a single file with some extra code (done once). That way you only link to the single file from your master page. As long as you don't have anything particularly heavy in there that's very reasonable.

For the id issue, change the jquery to search by css class.


original answer: ignore below, I thought it wasn't already in separate files.

Move it to a .js file, and change from using the id to use a class.

If you want to keep it structured / the .js code that's on a file dedicated to only that control's code, you can do so and add some extra code that joins all the various .js files in your site to a single .js file. This allows you to structure it as you please, without getting unnecessary extra requests.

柠栀 2024-09-26 03:55:17

我不确定这是否是 MVC 特有的问题。如果您想要一个真正独立的控件,请查看服务器控件(如复合控件或脚本控件)。这些控件不使用 ASCX 页面并且独立于 Web 应用程序。这样,您就可以将 CSS 表和 JS 页面嵌入到资源中。当您构建项目时,它会编译为单个 DLL。通过这种方式,您可以创建自己的控件库。但缺点是制作这些控件要困难得多。如果您有兴趣,请告诉我,我将发布一个示例。看一下这里的示例: http: //msdn.microsoft.com/en-us/library/aa719734%28v=vs.71%29.aspx

但是,如果您确实想使用 ASCX 页面嵌入 javascript 和 CSS,您可以这样做通过脚本管理器(好吧,我至少用Javascript而不是CSS完成了这个)。示例:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      dim myScript as new StringBuilder
      myScript.Append("function helloWorld(){" & vbcrlf)
      myScript.Append("alert('Hi')" & vbcrlf)
      myScript.Append("}" & vbcrlf)

      ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(),"MyScript",myScript.toString, false)
  End Sub

此示例创建一个简单的 javascript 函数文本并将其注册到页面。不,如果你有很多 JavaScript,这会变得很麻烦。另外,我不确定这是否适用于 CSS。让我知道这是否有帮助。

I'm not sure if this is a question specific to MVC. If you want a truly self contained control, look at server controls (like a composite control or script control). These controls do not use ASCX pages and are independent of a web application. This way, you can embed your CSS sheets and JS pages in a resource. When you build the project, it compiles to a single DLL. In this way, you can create your own library's of controls. The downside, though, is that making these controls are considerably more difficult. If you are interested, let me know and I'll post an example. Take a look here, for an example: http://msdn.microsoft.com/en-us/library/aa719734%28v=vs.71%29.aspx

But, if you really want to use ASCX page to embed the javascript and CSS, you can do it through the script manager (well, I've at least done this with Javascript, not CSS). Example:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      dim myScript as new StringBuilder
      myScript.Append("function helloWorld(){" & vbcrlf)
      myScript.Append("alert('Hi')" & vbcrlf)
      myScript.Append("}" & vbcrlf)

      ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(),"MyScript",myScript.toString, false)
  End Sub

This example creates a simple javascript function text and registers it with the page. No, if you have lots of javascript, this will get cumbersome. Also, I'm not sure this will work with CSS. Let me know if this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文