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 开发者_StackOverflow中文版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:
http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx
Is it of any help?
I've just posted a zip of a control library I have used a fair amount which embeds the scripts and has jquery UI themeing inbuilt.
It has a pretty comprehensive mapping of the ASP controls to Jquery + it handles the inclusion of scripts correctly so yoou don't run into the duplication .axd inclusion (ending up with multiple copites of scripts if you are not careful)
Note it also has a bunch of other DAL/Linq stuff in there + some generic hierachy server controls which are partially built (jTreeView clone) but I've used it pretty successfully in some largish projects
Link to sourceforge repository
精彩评论