如何像JS和脚本等结合成一个用户控件和评估ASP.NET页/负载 [英] How to combine scripts like JS and such into a UserControl and evaluate/load in ASP.NET pages

查看:120
本文介绍了如何像JS和脚本等结合成一个用户控件和评估ASP.NET页/负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个时间,我spended小时,天,发现了一个平原和平滑的方式来创建一个Web控件可以处理脚本加载。根据服务器侧的参数(如用户登录与否),则控制可以决定加载脚本到客户端或没有。

Once a time I spended hours and days to found a plain and smooth way to create a WebControl that can handle loading of scripts. Based on server side parameters (like user logged in or not), the control can decide to load script to the client or not.

我们叫它CombinedScripts。

We call it CombinedScripts.

昨天,我是在评论关于这个话题的问题。可悲的是,当我今天早上写了一个答案,它是由运营商删除。我是12分钟迟到,说错误的答复。现在,我在这里试着重新做的问题,听你(观众),也把我的答案,它接下来的15分钟在这个问题:这是当然可以自由之内答复,并给出出主意,我会接受,纪念最适合的答案,如果有人做了一些更好,然后我的。

Yesterday I was commenting a question regarding this topic. Sadly, while I wrote a answer to it this morning, it was deleted by the operator. I was 12 minutes late, said the error-reply. Now I try to re-make the question here, listen to your (the audience) and also put my answer to it within the next 15 minutes in this Q. It's of course free to reply and give ideas, i will accept-mark the best suited answer if someone did some better then mine.

谷歌表示,关于如何使这样的事情工作上的事情有非常大的范围。此外,还似乎在路上无数的一些棘手的部分。该脚本的路径,行为,当它不工作(因为它很容易编译)。还有一个web.config设置在项目注册的组件。问题是关于尽量使控制单机越好,嵌入资源,控制转移到其他项目中重复使用。

Google says a very big range of things about how to make such things work. There also seem to be some numerous tricky parts on the way. The path to the script, behavior when it not works (because it's easy to compile). There are also a web.config setup to register the component in the project. The question is about try make the control stand-alone as possible, embed the resources and move the control to other project for re-use.

推荐答案

这有一些技术上的棘手的部分,我尝试在这里介绍他们。我花了一些时间在谷歌之前,我终于得到了所有作品。我当然不能帮你与他们。其中有些是非常当地的实际情况。其实,我也从来没有发现任何合适的信息到我的场景中,并没有导致我到一个spaghetti- code,休斯code基地等。

This have some technically tricky parts and I try to cover them here. I spent some time on Google until I finally got all pieces. I surely can't help you with all of them. Some of them are very local to the actual scenario. In fact, I did never found any suitable information to my scenario, that didn't lead me to a spaghetti-code, hughe code base and so on.

您创建一个复合控件,只需在新类中重写System.Web.UI.Webcontrols.Webcontrol。我用CombinedScript作为名字由于你的兴趣。课程的名称可以是任何东西。

You create a Composite Control by simply override System.Web.UI.Webcontrols.Webcontrol in a new class. I use CombinedScript as a name due to your interest of it. The name can of course be anything.

public class CombinedScript : System.Web.UI.WebControls.WebControl
{

}

虽然这,你有一些方法覆盖使用。有几种方法,然后在下面的那些,但你必须谷歌最擅长的套房,您的需要。

While override that, you have some methods to use. There are several methods then those below, but you have to google on what best suites your need.

protected override void OnPreRender(EventArgs e)
{
}

protected override void Render(HtmlTextWriter writer)
{
}

的一点是,该方法的前做的事情,同时和之后生成的控制内容到客户端。上preRender之前发生,即。

The point is that the methods are doing things before, while and after the generate of the controls content to client. The OnPreRender occurs before, I.e.

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    ClientScriptManager cs = this.Page.ClientScript;
    cs.RegisterClientScriptResource(typeof(WebControls.CombinedScript), "Project.Script.Common.js");
    cs.RegisterClientScriptResource(typeof(WebControls.CombinedScript), "Project.Script.JQuery.js");
    cs.RegisterClientScriptResource(typeof(WebControls.CombinedScript), "Project.Script.JCharts.js");
}

这里的一个棘手的事情是通向脚本。 项目是DLL组装基地名称。 脚本是区分大小写的路径,其中的脚本文件的位置(从项目的基本路径计算)。最后一部分是文件的区分大小写的名称。我不知道,但我认为道路诀窍是毫无疑问的,如果你离开的确切根源一切。

A tricky thing here is the path to scripts. "Project" is the DLL assembly base name. "Script" is the case sensitive path, where the script files are located (counted from project base path). The last part is the case sensitive name of the file. I'm not sure, but I think the path trick is no question if you leave exact everything at root.

WebControls.CombinedScript是,你可以看到,该控件的名称。在ASP.NET页面中使用,它具有在web.config中进行注册。

WebControls.CombinedScript is, as you can see, the name of the control. To be used in ASP.NET pages, it has to be registered in web.config.

<pages>
  <controls>
    <add assembly="Project" tagPrefix="myname" namespace="Project.WebControls" />
  </controls>
</pages>

这些RegisterClientScriptResource使得脚本被加载 GET /WebResource.axd?d=MTVCK_diZif7wEDQp9UGv-GlqPKP-WXGPNEqr-mq_RnUKWDgSydkLxIuSusdnolRQsR-wpNbTpfJzAVtsonjuwVUqzguotspQFImk6-auSF3N18eVOXbYh_-6NHovgR90&t当网站被加载。如果他们不这样做,有什么不对。即走错了路,忘了包括JS作为嵌入的资源等。

Those RegisterClientScriptResource makes the scripts being loaded as GET /WebResource.axd?d=MTVCK_diZif7wEDQp9UGv-GlqPKP-WXGPNEqr-mq_RnUKWDgSydkLxIuSusdnolRQsR-wpNbTpfJzAVtsonjuwVUqzguotspQFImk6-auSF3N18eVOXbYh_-6NHovgR90&t when the site are loaded. If they don't, there are something wrong. I.e. wrong path, forgotten to include js as embedded resource and so on.

在上述所有完成后,你注册DLL,添加命名空间/控制标记,然后把它放在你的页面上使用&LT; MYNAME:CombinedScript =服务器/&GT;

When all above is done, you register the DLL, add the namespace / control tag and then put it on your page with <myname:CombinedScript runat="server" />

您也许可以做这样的事情,公共属性的复合控件添加搜索
公众诠释ScriptType {获取;设置;} 。把属性来控制&LT; MYNAME:CombinedScript ScriptType =1=服务器/&GT; 。并添加如果(this.ScriptType == 1){..evaluate加载脚本什么..} 。当然,这类型的评估可以从客户方(任何人都可以更换自己生成的html到另一个ScriptType ID)来改变。

You can probably do something like this, add public properties to the Composite Control
public int ScriptType {get;set;}. Put the attribute to the control <myname:CombinedScript ScriptType="1" runat="server" />. and add if (this.ScriptType == 1) { ..evaluate what script to load.. }. Of course that type of evaluation can be altered from clientside (anyone can replace their generated html to another ScriptType ID).

我想你可以通过这个更好地调查自己。你想计算结果为加载哪些脚本语句。上面的最后一个样本是一种方法,你当然可以把任何类型的逻辑之间/上RegisterClientScriptResources。

I think you can investigate yourself better by this. You want statements that evaluate what scripts to load. The last sample above is one way and you can of course put any kind of logic between / on the RegisterClientScriptResources.

我谈到RenderControl和渲染。结果
您可以使用它像这样,搜索结果

I was talked about RenderControl and Render.
You can use it like this,

protected override void Render(HtmlTextWriter writer)
{
    HttpResponseSubstitutionCallback callback = new HttpResponseSubstitutionCallback(outputcache_callback);
    Context.Response.WriteSubstitution(callback);
}

public static string outputcache_callback(HttpContext context)
{
    string html = "";

    html += "<script type='text/javascript'>";
    html += " var lang='" + Project.Common.CurrentLang + " ';";
    html += " var applicationPath='" + GetApplicationPath + "'; ";
    html += "</script>";
    return html;
}

public GetApplicationPath { get { ..logic.. }}

这是另一种技术投放到脚本的客户方。好的,如果你有哪些加载到你的脚本用户或特定实例变量。 静态是替代样本。另外,如果你想影响的控件本身的内容发生(例如,如果你做出更明智的asp:标签,你可以这样做,

That's another kind of technique put scripts into clientside. Good if you which to have user- or instance-specific variables loaded to your scripts. static is for the substitution-sample. Also, if you want to affect the generating of content of the control itself (like if you would make a smarter asp:Label, you can do so,

public override void RenderControl(HtmlTextWriter writer)
{
    base.RenderControl(writer);
    writer.Write("<div id='dvBox'>");
    ....
    base.RenderChildren(writer);
    writer.Write("</div>");
    writer.Write(script);
}

我希望是结合并帮助多个开发人员那里寻求这样的方式来建立控制信息。

I hope that are information that combine and help several developers out there searching for such way to build controls.

这篇关于如何像JS和脚本等结合成一个用户控件和评估ASP.NET页/负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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