开发者

Calling method on Master page from WebMethod

开发者 https://www.devze.com 2023-01-10 11:18 出处:网络
Or any viable workaround. So, imagine a开发者_如何学JAVA Master page that implements IFooMaster with a method ShowFancyMessagePopupTheBusinessCantLiveWithout(string message);

Or any viable workaround.

So, imagine a开发者_如何学JAVA Master page that implements IFooMaster with a method ShowFancyMessagePopupTheBusinessCantLiveWithout(string message);

I am in a page that inherits this master page. Upon a checkbox being unchecekd, I want to show a message to the user that if they save it, they can't re-check the checkbox without some admin action.

I've been given feedback that I can't just use an alert('message'); in javascript because they want the consistent look of these messages.

Next, I tried to make an ajax call via PageMethods (as that's what everything else in this codebase uses) to show a message. My problem lies in this method being static.

[WebMethod]
public static void ShowSuperImportantMessage()
{
     if(!checkboxICareAbout.Checked)
         ((IFooMaster)Master).ShowFancyMessagePopupTheBusinessCantLiveWithout("If you uncheck that thing, you can't recheck it.");
}

Since ShowSuperImportantMessage() is static, I can't access Master from within.

The method on the master page looks more or less like this:

public void ShowFancyMessagePopupTheBusinessCantLiveWithout(string message)
{
    lblGenericMessage.Text = message;
    btnGenericMessageOK.Focus();
    upGenericMessage.Update();
    mpeGenericMessage.Show();
}

mpeGenericMessage is an ajaxtoolkit:ModalPopupExtender.

upGenericMessage is an update panel.

The other 2 are obvious.

Any ideas? Can I do some jQuery kung-fu to show that stuff? I tried, but the solution complained that the controls I tried to refer to by ClientID didn't resolve since they were on the Master page.

quick edit: Before anyone tells me the architecture is a problem, or I shouldn't have put such a thing on a master page, or w/e...

I know the situation is not ideal, but I this is inherited code, and I can't drop it all and rewrite half of their web stack.


Try something like this (untested):

((IFooMaster) ((Page)HttpContext.Current.Handler).Master)

It appears this doesn't work - Master isn't hooked up when the PageMethod is called (makes sense).

So, instead, create an empty page using the same master page. Have that page accept either a POST or GET with whatever parameters you need to pass to your master-page method. Have the Page_Load extract the parameters and call the method. It should then use Response.Write to return a result (and remember to change the Content-Type). Have your client-side code call the page and get the result.


Did you try something like window.top before the ClientID?

Per comments You don't need to hardcode ClientID. Since your js is in page, try something along the following lines....

window.top.document.getElementById( "<%= yourelement.ClientID %>" ).Whatever();


Sorry to take so long to respond/answer.

I'm not proud of this at all, mind you, but the eventual solution was to hardcode the client IDs into the jQuery that pulled up the modal dialog on the master page.

Like I said, I'm not proud of this dirty, dirty fix. However, the consolation is that, since it's on the master page, there isn't really any naming container above it. As such, it's much less likely to run into problems with the clientID changing.

0

精彩评论

暂无评论...
验证码 换一张
取 消