简体   繁体   中英

Problem with Finding Controls of a page that uses a Master Page

I have a set of pages, each page is used to prompt the user for data and create a record for this data in the database.

I use the same technique for all the pages .. 2 panels, the first has all the input controls and is visible by default and a second panel that shows a success message and is hidden by default.

Now when the user clicks the "Submit" button .. if the data is valid, I write a couple of lines to hide the first panel and show the first one .. I have like 20 pages that uses the same technique so as a good practice I added a class to my web project that's called Helper that searches for the 2 panels, then hide and show what I need.

I used the same panel Id for all the pages so I could use it easily!

NB: I first just went ahead and searched for the panel directly but it won't work so I tried to search for the content control that holds the panels and stuff (as this page is using a master page) and then find the Panel. both attempts won't work out!

public class Helpers
{
    public static void SuccessfulSubmission(Page p)
    {
        Content content = p.FindControl("Content2") as Content;
        Panel inputPanel = content.FindControl("inputPanel") as Panel;
        inputPanel.Visible = false;
    }
}

Searching content control would never work because whenever master page is used, its controls get inserted into the page and page's controls (within content control) gets added to the corresponding content placeholders. Perhaps, you should try to locate the content placeholders first and then try finding the control.

However, a better design would be creating a base page class along with specific master page to control this interaction. For example,

You will have a master page (say named as TwoPanelMaster)that would use your site master page and use two panel layout along with submit button. Idea is you have standard layout that is controlled via master page.

  <asp:Panel runat="server" ID="InputPanel">
     <asp:ContentPlaceholder runat="server" ID="Input"></asp:ContentPlaceholder>
  </asp:Panel>
  <asp:Panel runat="Server" ID="Message">
     <!-- put the UI for displaying message -->
  </asp:Panel>
  <asp:Button runat="server" ID="Submit" />

Expose methods in the master code behind to get access to submit button, to show/hide panels etc.

Now create a base page class that would code the basic interaction -

public abstract class InputPage : System.Web.UI.Page
{
  protected override OnLoad(EventArgs e)
  {
     var master = this.Master as TwoPanelMaster;
     master.GetSubmitButton().OnClick += Submit_Click();
  }

  private void Submit_Click(object sender, EventArgs e)
  {
     ValidateInput();
  }

  protected virtual void ValidateInput()
  {
     // do some common code (if any)
     string message;
     if (OnValidateInput(out message))
     {
        // validation succesful, show success message
        var master = this.Master as TwoPanelMaster;
        master.ShowMessage(message); // this method should hide the input panel and display message panel.
     }
  }

  // pages should provide implementation of this method
  protected abstract bool OnValidateInput(out string successMessage);

}

This is a general approach that would give you a more control and remove dependency such that your pages must have panels having particular ID etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM