Calling javascript for a SharePoint webpart?

2 posts, 0 answers
  1. Steve Holdorf
    Steve Holdorf avatar
    119 posts
    Member since:
    Apr 2010

    Posted 21 May 2012 Link to this post

    We have your asp .net ajax web controls but do not have your Sharepoint controls. It looks like we will get them sometime after October of this year. In the mean time how do I call javascript from a SharePoint WebPart. The WebPart is just a user defined control ascx file. Where do I register the script section and where do I put the functions?

    I was told:

    You can create a Litral control and set the Text property, like following -

    protected override void CreateChildControls()

    {

    Litral scriptLitral=new Litral();

    scriptLitral.Text="<script type='text\javascript'>";

    scriptLitral.Text+="function CustomAlert(msg){";

    scriptLitral.Text+="alert(msg);}</script>";

    Controls.Add(scriptLitral);
    }


    I tried this but could not add the function to a button control. Can you help?
     
    Thanks,
     
    Steve Holdorf
  2. Rumen
    Admin
    Rumen avatar
    12846 posts

    Posted 23 May 2012 Link to this post

    Hi,

    The code is correct except that you have a typo in the control literal name:

    private RadButton button = null;
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                ScriptManager sm = ScriptManager.GetCurrent(Page);
                if (sm == null)
                {
                    //add a new script manager if one has not been registered yet.
                    sm = new ScriptManager();
                    sm.ID = "ScriptManager1";
                    Controls.Add(sm);
                }
                button = new RadButton();
                button.OnClientClicking = "CustomAlert";
                button.ID = "RadEditor1";
                Controls.Add(button);

                Literal scriptLitral = new Literal();
                scriptLitral.Text="<script type='text/javascript'>";
                scriptLitral.Text+="function CustomAlert(msg){";
                scriptLitral.Text+="alert(msg);}</script>";
                Controls.Add(scriptLitral);

            }

    For your convenience I have attached my test web part.

    All the best,
    Rumen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

Back to Top