We help IT Professionals succeed at work.

Ajax postback control pageload function

gabi456
gabi456 asked
on
Hello

In the code attached, there is a function which disables some buttons.

What is needed is that this function not be called if the postback comes from an asp.net radiobuttonlist that is inside a listview.

Or another would be to only call this function first time the listview is loaded and not when further ajax postbacks are done after e.g. selection an item in the radiobuttonlist (which calls a SelectedIndexChanged in the code behind).
function pageLoad() {
            var manager = Sys.WebForms.PageRequestManager.getInstance();
            manager.add_endRequest(OnEndRequest);

            var elem = document.getElementById('<%= DataPagerListView.ClientID %>').getElementsByTagName("*");
            for (var i = 0; i < elem.length; i++) {

                if (!elem[1].disabled) {
                    disable();
                }

            }

        }

Open in new window

Comment
Watch Question

Rahul GadeSr. Architect

Commented:

You need to add a condition in the pageLoad() as

if ( ! Page.isPostBack)
{
  //Put the code that you do not want to get executed for ajax postback

}

-Rahul Gade

Author

Commented:
Hello Rahul

Sorry, it didn't work.

I want it be when it is posted back, but not from the radiobuttonlist control.

Thanks
Rahul GadeSr. Architect

Commented:

Ohh, sorry I didn't realized that you want to apply this restriction on client side code, page.IsPostBack is available on code behind only.
In order to have better control on what script should be executed when loading page you can use page.registerScriptBlock("AA","<script langauge=JavaScript>your script goes here</script>.

So, you can register your javascript code only when needed at runtime (page loading), instead of having this as a part of page always.


-Rahul Gade
Rahul GadeSr. Architect

Commented:
Instead of registerScriptBlock, you can also use page.registerStartupScript function to immediatly reflect the script code.

Author

Commented:
Hello

This still doesn't solve the problem. Even if i register the script from the code behind it will still run when the radiobuttonlist is selected.

Need to put something in the javascript script code attached to not call the function if the postback was done from the radiobuttom list selectedindexchanged event.

Thanks.

Commented:
before registering the script, you just check which control causes the postback, if the postback is raised from the radiobutton, then do not register the script.

here is a method to findout which control causes the postback

http://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event
Sr. Architect
Commented:
Ok, in case you have multiple places on page causing the postBack and you want to restrict function invokation only for a specific condition, then you can use hidden control like textbox to maintain the flag. You can set the value of this flag using javascript, while you will have to retrive this value on server side using request object as this textbox won't be a server side control.

While, I still belive you can achive the same using registerStartupScript and IsPostBack (there are different ways to detect the same). But for multiple location causing the similar async post back situation, hidden control should do the trick for you.

-Rahul

Author

Commented:
Thanks