Kevin Griffin
Kevin Griffin

Reputation: 2257

Call onresize from ASP.NET content page

I have a JavaScript method that I need to run on one of my pages, in particular, the onresize event.

However, I don't see how I can set that event from my content page. I wish I could just put it on my master page, but I don't have the need for the method to be called on all pages that use that master page.

Any help would be appreciated.

Upvotes: 4

Views: 6841

Answers (3)

Adhip Gupta
Adhip Gupta

Reputation: 7163

How about use code like the following in your Content Page (C#)?

Page.ClientScript.RegisterStartupScript(this.GetType(), "resizeMyPage", "window.onresize=function(){ resizeMyPage();}", true);

Thus, you could have a resizeMyPage function defined somewhere in the Javascript and it would be run whenever the browser is resized!

Upvotes: 0

Gordon Thompson
Gordon Thompson

Reputation: 4844

I had the same problem and have come across this post :

IE Resize Bug Revisited

The above code works but IE has a problem where the onresize is triggered when the body tag changes shape. This blog gives an alternate method which works well

Upvotes: 0

Jason Bunting
Jason Bunting

Reputation: 58931

Place the following in your content page:

<script type="text/javascript">

// here is a cross-browser compatible way of connecting 
// handlers to events, in case you don't have one
function attachEventHandler(element, eventToHandle, eventHandler) {
    if(element.attachEvent) {
       element.attachEvent(eventToHandle, eventHandler);
    } else if(element.addEventListener) {
       element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false);
    } else {
    element[eventToHandle] = eventHandler;
  }
}

attachEventHandler(window, "onresize", function() {
    // the code you want to run when the browser is resized
});

</script>

That code should give you the basic idea of what you need to do. Hopefully you are using a library that already has code to help you write up event handlers and such.

Upvotes: 4

Related Questions