1

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: Story of a mischievous Page Viewer Web Part.

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?

3 Answers 3

2

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. :(

3
  • Does your script actually run, or is it loaded but no methods called? Commented Feb 9, 2011 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... Commented Feb 10, 2011 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 Commented Feb 10, 2011 at 14:47
1

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).

1
  • 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 Commented Feb 7, 2011 at 22:05
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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