Search code examples
c#asp.netmaster-pagesbusiness-logic-layer

C# access element in master page from logic layer


I have a content page connected to a master page. I can access an element on the master page and modify it directly from the content page .cs file by calling a method on the site master. (this is probably the most standard bug people have in this type of area)

My problem is that I wanted to extend this functionality to update the site master page from an AJAX request as well. The ajax file calls a different page which in turns starts an instance of the logic layer which I use for all the calculations and connections. What I am trying to do is access the sitemaster directly from the logic layer (only a .cs file).

My current code is this:

SiteMaster sm = new SiteMaster();
sm.MyMethod("param1", "param2");

This successfully accesses the method called "MyMethod" in the site master but inside this method I have this code:

mySpan.InnerText = "this is a test";

which doesn't work because I get the "Object refernce not set to an instance of an object...." error. This is because mySpan is NULL. If I call it using this.mySpan.InnerText though, if I hover over "this" then I can see the ID "mySpan".

Does anyone know how I can get around this problem? Every search I have made is regarding people who want to access the elements from the content page which already works for me.


Solution

  • I believe you've got a misunderstanding here. If I understand correctly you've got a page with a MasterPage. On that aspx page you're doing an ajax call (perhaps to a WebService) which does something like:

    [WebMethod]
    public void UpdateText(string message)
    {
      var master = new SiteMaster();
      master.mySpan.Text = message;
    }
    

    There are a couple of things wrong here.

    When you use this approach is an aspx page you're updating that Page's master. For example:

    public void OnSomeRandomButtonClick(object sender, EventArgs e)
    {
      ((SiteMaster)this.Page.Master).mySpan.Text = "Some Text";
    }
    

    What you're doing here is updating the span on the master page before it's being sent to your browser. The other subtly is that you're not creating a new SiteMaster, you're using the Page's existing Master and casting it to a SiteMaster.

    There are a couple of reasons you can't do this with ajax:

    • A webservice doesn't have a MasterPage
    • By the time you send an ajax request your Master page has already been created and sent to the browser.

    So your question becomes how do we update a span in the Master without posting back to the server?

    Lets look at the html which is actually on your box, it will look something like this:

    <html>
      <head>
        <title>My Awesome Page</title>
      </head>
      <body>
        <h1>This is my Awesome Website</h1>
        <span id="mySpan">I'm sure you'll like it</span>
        <div>
          <p>Page Content</p>
        <div>
      </body>
    </html>
    

    Lets assume that everything here is generated by the master and only the <p>Page Content</p> is your aspx page (There will also be loads of ASP.NET junk added, we'll ignore that for the time being).

    What you want to do is update the text in mySpan without posting back to the server. You can do this via the javascript - don't get ajax involved at all!

    I'm going to assume you're using jQuery (mostly because I'm more familiar with it that plain old JS). You've got the ID of your span ("mySpan") so the rest is easy:

    $('#mySpan').html('This is the updated message');
    

    You can put this in either a click or a page load.