简体   繁体   English

将Javascript / JQuery存储在独立的C#类中

[英]Storing Javascript/JQuery in a Self-Contained C# class

I've written a Web Control as a C# class and I want to store the CSS and JavaScript within the class itself so that when the control is generated the CSS and JS is written to the page. 我已经将Web控件编写为C#类,并且希望将CSS和JavaScript存储在该类本身中,以便在生成控件时将CSS和JS写入页面。 This is to allow me to distribute the DLL to others in my organization as 1 file, vs distributing it with an external stylesheet and external .js file. 这是允许我将DLL以1个文件的形式分发给组织中的其他人,而不是使用外部样式表和外部.js文件进行分发。 This is curiosity, and not a requirement. 这是好奇心,而不是必要条件。

Below is the basic code for the Web Control. 下面是Web控件的基本代码。 It creates the number of DIV Containers specified in the "Columns" property of the class. 它创建在类的“列”属性中指定的DIV容器数。 The CSS formats the DIV elements, and the JavaScript/JQuery allows the users to drag & drop the columns in their preferred order. CSS格式化DIV元素,并且JavaScript / JQuery允许用户按照自己喜欢的顺序拖放列。

It's added to the page with this line: 它使用以下行添加到页面中:

<FOO:WidgetPanel runat="server" ID="SamplePanel" />

and the Columns property is set in the Page_Load event: 并在Page_Load事件中设置Columns属性:

SamplePanel.Columns = 3;

And here is the actual class: 这是实际的类:

namespace WebWidgets
{
    [ToolboxData("<{0}:WidgetPanel runat=server><{0}:WidgetPanel")]
    public class WidgetPanel : WebControl
    {
        [Browsable(false)]
        public int Columns { get; set; }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            string html = "";

            for (int i = 1; i <= Columns; i++)
            {
                html += "<div  id=\"column" + i + "\" class=\"column\">";
            }

            writer.Write(html);
        }
    }
}

I think that using external files is better, but I have been asked by my management to provide a self-contained .DLL as opposed to 3 seperate files. 我认为使用外部文件会更好,但是管理层要求我提供一个独立的.DLL,而不是3个单独的文件。 (They also want the GUI developed before the framework so they can "see" what it looks like... Got to love it!) (他们还希望在框架之前开发GUI,以便他们可以“看到”它的外观……喜欢它!)

Is it possible? 可能吗? And if so, links to a tutorial/example would be greatly appreciated. 如果是这样,将非常感谢您链接到教程/示例。

You can add a resource file under the properties in your project, add the js and css files to it and read it using Properties.{ResourceFileName}.{YourResourceKey}. 您可以在项目的属性下添加资源文件,向其中添加js和css文件,然后使用Properties。{ResourceFileName}。{YourResourceKey}进行读取。 Please note that you have to change the resource type to Embedded resource to embed it in the dll. 请注意,您必须将资源类型更改为Embedded resource才能将其嵌入dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 例如 C# 自包含 getter - C# Self-contained getter for instance 如何在内部使用IronPython引用自包含的C#类库项目(Visual Studio 2010) - How to refer self-contained C# class library project with IronPython inside (Visual Studio 2010) C#.NET 4自包含的Oracle驱动程序部署 - C# .NET 4 Self-Contained Oracle Driver Deployment 简单的自包含SNMP代理示例? (java / c#ideal) - Simple self-contained SNMP Agent example? (java/c# ideal) 发布用于Linux体系结构的.NET Core C#自包含应用程序 - Publishing .NET Core C# self-contained application for linux architecture 如何创建一个用C#编写的独立,自包含的安装程序? - How to create a standalone, self-contained setup program written in C#? 如何发布引用另一个可执行文件的独立 C# 可执行文件? - How to publish a self-contained C# executable that references another executable? 独立的普通纪念品 - Self-contained generic memento 引用的项目是一个非独立的可执行文件。 自包含的可执行文件不能引用非自包含的可执行文件 - The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable 获取自带EXE的路径? - Get self-contained EXE's path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM