Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I found some JavaScript code on the web that I want to add to my custom web part so that I can modify a PageViewerWebPart's height to 100%. I just need to know what steps to take to call it. I added the JavaScript file to Visual Studio and called it Height100Percent.js and changed it to an embedded resource.

I think I need to use this to access the file from my C# code:

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Height100Percent.js");

But, I can't find out what method I need to call to execute my JavaScript file. What methods do I call, and should I include this code in CreateChildControls() or a different overridden method in the custom web part?

Below is something that I think resembles what should be done:

I added to my AssemblyInfo.cs:

[assembly: System.Web.UI.WebResource("ReportingMDMWebPart.Height100Percent.js", "text/js", PerformSubstitution = true)]

Created a method called RunJavaScript() which is called from the overridden CreateChildControls() method in my web part:

    private void RunJavaScript()
    {
        Type resourceType = this.GetType();
        string resourceName = "ReportingMDMWebPart.Height100Percent.js";

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager csm = Page.ClientScript;

        // Register the client resource with the page.
        csm.RegisterClientScriptResource(resourceType, resourceName);
    }

My Height100Percent.js file is a duplicate of file on this page: http://blogs.msdn.com/b/malag/archive/2008/09/15/story-of-a-mischievous-page-viewer-web-part.aspx

But, I can't get the JavaScript to run at all. I've searched around for this quite a bit, but I can't seem to find anything that explains this process at all. Is there anything unusual about what I tried above?

share|improve this question

3 Answers 3

Depending on what you're trying to achieve, look at the ClientScriptManager of the Page object (Page.ClientScript) has the methods you need (RegisterClientScript, RegisterClientScriptBlock).

share|improve this answer
    
I suggest you first confirm that you can load the resource correctly using the name you've indicated (separate from Javascript). Alternatively turn the JS into a deployed file and reference it using one of the other ClientScriptmanager methods –  Neil Richards Feb 7 '11 at 22:05
up vote 1 down vote accepted

This works:

Add to AssemblyInfo.cs

[assembly: WebResource("ReportingMDMWebPart.Height100Percent.js", "text/javascript")]

Add to web part:

ClientScriptManager csm = Page.ClientScript;
csm.RegisterClientScriptResource(this.GetType(), "ReportingMDMWebPart.Height100Percent.js");

Rename the string for the javascript file's project path to your own in each code block.

I still can't for the life of me get my page viewer to 100% page height, though. :(

share|improve this answer
    
Does your script actually run, or is it loaded but no methods called? –  Neil Richards Feb 9 '11 at 22:12
    
It does run; I threw in alert() methods to make sure. However, I always get a lot of nulls or undefineds when using getElementById, getElementsByTagName, etc... –  Matt Feb 10 '11 at 14:39
    
I've also tried modifying the HTML live through trial and error and haven't been able to get the PageViewer to expand to 100% page height. I'm sure it's possible, but after 45 min of manipulating tables and divs I was getting a headache :) . So even if my JS or CSS run, I still haven't figured out the exact algorithm needed anyway –  Matt Feb 10 '11 at 14:47

Have you explored the option of using jQuery. Otherwise you can just prepare the JavaScript file that you want to load and then upload it to the SharePoint site.

If possible add a reference of your javascript file in the web part, otherwise add a content editor web part and provide link of your javascript file in the content link property of webpart.

  • Sudhir
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.