tags:

views:

10

answers:

0

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>")] 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")]