2

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")]
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
giri-net
  • 97
  • 2
  • 12

2 Answers2

1

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?

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • http://msdn.microsoft.com/en-us/library/system.web.ui.webresourceattribute(v=VS.100).aspx and http://support.microsoft.com/kb/910442 are useful too – Colin Jul 07 '11 at 12:24
0

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

Paul Sullivan
  • 2,865
  • 2
  • 19
  • 25