如何嵌入jQuery库在asp.net中自定义服务器控件? [英] How to embed jquery library in asp.net custom server control?
本文介绍了如何嵌入jQuery库在asp.net中自定义服务器控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我emebeded在自定义服务器控件jQuery库。但它不工作。它抛出对象预期的错误。完整的code房源如下。
jQuery的-1.4.1.js是其重命名为jquery.js和
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Linq的;
使用System.Text;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间ServerControl1
{
[DefaultProperty(文本)]
[ToolboxData(< {0}:ServerControl1 RUNAT =服务器>< / {0}:ServerControl1>中)
公共类ServerControl1:WebControl的
{
[可绑定(真)
[类别(外观)]
[默认值()]
[本地化(真)
公共字符串文本
{
得到
{
字符串s =(字符串)的ViewState [文字];
返回((S == NULL)[+ this.ID +]:秒);
} 组
{
的ViewState [文字] =值;
}
} 保护覆盖无效RenderContents(HtmlTextWriter的输出)
{ output.Write(&所述p为H.;的Hello World !!&下; / P>中);
} 公共静态无效RegisterJQuery(ClientScriptManager CS)
{
cs.RegisterClientScriptResource(typeof运算(ServerControl1)
ServerControl1.Resources.jquery.js);
}
保护覆盖无效在preRender(EventArgs的发送)
{
如果(!this.DesignMode)
{ //注册的JavaScript库
ClientScriptManager CS = this.Page.ClientScript;
ServerControl1.RegisterJQuery(CS); }
} 保护覆盖无效的OnInit(EventArgs的发送)
{ 字符串的javascript =<脚本类型=文/ JavaScript的'>中+
$(文件)。就绪(函数(){+
警报($('P')文本());+
});< / SCRIPT>中; 如果(!(Page.ClientScript.IsClientScriptBlockRegistered(bhelp)))
Page.ClientScript.RegisterStartupScript(this.GetType(),bhelp的JavaScript); base.OnInit(E);
} }
} [大会:System.Web.UI.WebResource(ServerControl1.Resources.jquery.js,文/ JavaScript的)]
解决方案
Thjis文章似乎来形容你正在尝试做的:
<一个href=\"http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx\" rel=\"nofollow\">http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx
是不是有帮助?
I emebeded jquery library in custom server control. but it's not working. it throws "object expected error". the complete code listing is given below.
jquery-1.4.1.js is rename it to jquery.js
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ServerControl1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<p>Hello World!!</p>");
}
public static void RegisterJQuery(ClientScriptManager cs)
{
cs.RegisterClientScriptResource(typeof(ServerControl1),
"ServerControl1.Resources.jquery.js");
}
protected override void OnPreRender(EventArgs e)
{
if (!this.DesignMode)
{
// Register the JavaScript libraries
ClientScriptManager cs = this.Page.ClientScript;
ServerControl1.RegisterJQuery(cs);
}
}
protected override void OnInit(EventArgs e)
{
string javascript = "<script type='text/javascript'> " +
"$(document).ready(function () { " +
"alert($('p').text()); " +
"});</script>";
if (!(Page.ClientScript.IsClientScriptBlockRegistered("bhelp")))
Page.ClientScript.RegisterStartupScript(this.GetType(), "bhelp", javascript);
base.OnInit(e);
}
}
}
[assembly: System.Web.UI.WebResource("ServerControl1.Resources.jquery.js", "text/javascript")]
解决方案
Thjis article seems to describe what you are trying to do:
Is it of any help?
这篇关于如何嵌入jQuery库在asp.net中自定义服务器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文