This is a migrated thread and some comments may be shown as answers.

Calling javascript for a SharePoint webpart?

1 Answer 83 Views
WebParts for SharePoint
This is a migrated thread and some comments may be shown as answers.
Steve Holdorf
Top achievements
Rank 1
Steve Holdorf asked on 21 May 2012, 12:00 PM
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

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 23 May 2012, 04:37 PM
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.
Tags
WebParts for SharePoint
Asked by
Steve Holdorf
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or