[英]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.