views:

115

answers:

3

Or any viable workaround.

So, imagine a 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.

A: 

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();
BioBuckyBall
no dice. Maybe if I hardcode the clientID directly in the javascript, but I REALLY don't want to do that.
Andy_Vulhop
@Andy_Vulhop is the js in the page or a separate file?
BioBuckyBall
@Lucas Heneks in page.
Andy_Vulhop
A: 

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.

John Saunders
Almost. ((Page)HttpContext.Current.Handler) had me excited, but then .Master came back null.
Andy_Vulhop
@Andy: That's too bad. I guess the pagemethod doesn't go through the appropriate page lifecycle to get `Master` hooked up.
John Saunders
A: 

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.

Andy_Vulhop