none
Applying Master Page breaks query

    Question

  • Following the excellent advice at http://www.homepagedoctor.com I was able to create ASPX pages that write to an Access database and allow the user to displaying the entire database or by query based on name or account number.

    I setup a Master Page and appliedit to the individual ASPX pages.

    Everything EXCEPT the search page works with the master page applied. On the search page I get back No results.

    The Master Page uses a table to hold the FORM that prompts for the query values. The WHERE clause refers back to the values entered in that FORM

    Does the name of the fields defined in the form have to be changed in the WHERE clause?

     

    Friday, January 21, 2011 5:26 PM

Answers

  • Clark, of course you don't see the WHERE clause in the rendered page.

    All you see in the rendered page is what is sent to the browser, after asp.net does its thing.

    Your textbox name and id are there in the search page - scroll down a bit.

    To verify it, just do what I said:  hard code the rendered textbox "name" (note I corrected myself as I mis-spoke and said "id" in the first reply) from the search page into the Formfield parameter for your WHERE clause in Code View of your results page.  It will now work.

    • Proposed as answer by ClarkNK Monday, January 24, 2011 3:45 AM
    • Marked as answer by cduchon Monday, January 24, 2011 6:39 PM
    Sunday, January 23, 2011 2:38 PM
  • OK I think I have a robust way of making this work with a Master Page

    The problem is that we have a textbox (I give it ID of  TxtBxHSLName ) inside the contentplacehoder1 of a MasterPage (which is where all the controls of child pages will be) and we want to access the value of that textbox in the WHERE clause when configuring a Gridview datasource on a different page.

    The solution that works for me is:

    1. On the page where your gridview is, put a textbox with the same ID as on the search page: TxtBxHSLName

    2. Then set your gridview as usual, configure its datasource as usual, and set up the WHERE to depend on the value of  TxtBxHSLName

    3. The last step you need some code to run when the Results page loads that assigns the value to TxtBxHSLName that is the same as was entered in that textbox on the search page. If you use VB, that code looks like:

    -----------

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.PreviousPage Is Nothing Then
                Dim placeHolder As Control =
                    PreviousPage.Controls(0).FindControl("ContentPlaceHolder1")
                Dim SourceTextBox As TextBox =
                    CType(placeHolder.FindControl("TxtBxHSLName"), TextBox)
                If Not SourceTextBox Is Nothing Then
                    TxtBxHSLName.Text = SourceTextBox.Text
                End If
            End If

        End Sub

    -------------

    This code digs into the previous page with a find.control to get the value of the textbox on that page and assign that to the textbox on the results page. So I think it will be pretty robust.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    • Proposed as answer by ClarkNK Tuesday, January 25, 2011 1:20 PM
    • Marked as answer by cduchon Wednesday, January 26, 2011 12:32 AM
    Monday, January 24, 2011 9:21 PM

All replies

  • Some additional information... It looks like I lose my <FORM> tag when I apply the Master Page.

    If I manually add the <form id="form1" runat="server"> back in I get an error saying that only one runat="server" can be run per page.

    I've tried naming the forms different names, removing the <FORM> statement from the Master Page, and removing the "runat=server in the query page but the problem persists.

    Is there something special I have to do top define a FORM in the Master Page?

    The original code (without Master Page) works and looks like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Page Language="C#" %>
    <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled 1</title>
    </head>
    <body>
    <form id="form1" runat="server">
    Case Number
    <asp:TextBox runat="server" id="Acct"></asp:TextBox>
    <br />
    <br />
    <asp:Button runat="server" Text="Search Acct" id="Button1" PostBackUrl="acct.aspx" />
    <br />
    <br />
    Name
    <asp:TextBox runat="server" id="Acctname"></asp:TextBox>
    <br />
    <br />
    <asp:Button runat="server" Text="Search Acct Name" id="Button2" PostBackUrl="acctname.aspx" Height="26px" />
    <br />
    <br />
    </form>
    </body>
    </html>

    After I apply the Master Page code looks like this:

    <%@ Page Language="C#" masterpagefile="MyMasterPage" %>

     

    <asp:Content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
    Case Number
    <asp:TextBox runat="server" id="Acct"></asp:TextBox>
    <
    br />
    <
    br />
    <
    asp:Button runat="server" Text="Search Acct" id="Button1" PostBackUrl="acct.aspx" />
    <
    br />
    <
    br />
    Name
    <asp:TextBox runat="server" id="Acctname"></asp:TextBox>
    <
    br />
    <
    br />
    <
    asp:Button runat="server" Text="Search Acct Name" id="Button2" PostBackUrl="Acctname.aspx" Height="26px" />
    <
    br />
    <
    br />
    </asp:Content>

     

     

    Friday, January 21, 2011 7:02 PM
  • It's actually hard to know without seeing the master page. Your page that uses the master looks correct. You shouldn't have a Form element in the child page since it should already be defined within the master page. The child page basically just gets inserted into one of the contentplaceholders in the master, so there is only one form element needed. Can you post some of the master page so we can get a feel for it and maybe we can spot the problem?

    Friday, January 21, 2011 9:11 PM
  • Master Page is below. It's pretty basic for now. In looking at this it seems as though the value for ACCTNAME and ACCT isnt getting passed to the PostBack page properly.

    I have configured the SELECT in the PostBack page to look for teh ACCTNUM field in the database being = the ACCTNUM form field

    As I said originally the query works fine if the FORM tag is present in the search page and there is NO Master Page attached.

    Do I have to do anything different if formatting the query to retriev the form field when I am using a master page?

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Master Language="C#" %>
    <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled 1</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
    </head>

    <body>

    <form id="form1" runat="server">
     <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
     </asp:ContentPlaceHolder>
    </form>

    </body>

    </html>

    Friday, January 21, 2011 9:35 PM
  • It's kind of a best practice when you first create an aspx page to attach it to the Master Page right away.

    Then any controls you put on the page "know" where they are.

    I re-configure your controls on such a page, you should not run into any trouble.

    OK, and here's my soapbox thingy: You are always (IMHO) opinion going to be better off first setting up any database-oriented site using Visual Web Developer Express.  It immediately offers you the opportunity to associate any new page with a Master Page if desired, and automatically creates the code-behind page which, sooner or later, you will wish you had if you get into anything more than the most vanilla use of your database.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Friday, January 21, 2011 10:19 PM
  • First - thanks for a great website at HomePageDoctor.com !

    I wouldnt have gotten this far without it!

    I tried recreating the query page from scratch based on the Master Page but I get the same results - No Records Found by the query

    Do I need to do something different when I setup the query on the postback page?

    Is there an easy way to display the values getting based from the form/query page to the PostBack page?

    Sunday, January 23, 2011 12:01 AM
  • Thank you for the nice comments.

    Now to your issue -- I am not entirely sure how you have things set up, but it sounds as if you are putting some controls on the Master Page itself?

    Normally the way you would set things up is to make a master page with some stuff on it that you plan to be common shared material like navigation menu etc. for all your pages, and have a content placeholder  region in it.

    Than you would make some aspx pages that are attached to the Master Page and on those aspx pages, the only place where you would be able to place controls would be in that content placeholder section.

    Now, if you set things up that way, and make two aspx pages, one named search.aspx and another results.aspx, then you should be able to follow tutorial number 3 to do your search on one page and get your results on another.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Sunday, January 23, 2011 2:20 AM
  • Hmm hold on there pardner!

    When I did the tutorial I did not use a Master page, so I just now went to repeat the process but using a Master Page, and guess what -- I got the same resul you did -- nothing showed up on the results page.

    I'll have to research this a bit ---



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Sunday, January 23, 2011 3:15 AM
  • OK well this is pretty wierd. I have posted two pages to my homepagedoctor site so you all can check it..

    The first page uses the master page: http://www.homepagedoctor.com/testSearch.aspx

    Enter the name Kurtz into the textbox and press the button, it takes you to a results page showing no data

    The second page does not use the Master page: http://www.homepagedoctor.com/testSearchNoMaster.aspx

    Again enter the name Kurtz into the textbox and press the button. Up comes the result as it should.

    I have no idea why it is not working with the Master Page. The code is pretty much identical. If anyone can spot what is wrong, let me know. I think I will head over to the asp.net forum with this one !



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Sunday, January 23, 2011 4:40 AM
  • If you use Master pages you have to understand how they work.  Look at the source when previewing in a browser.  Look at the id names.

    I assure you that the texbox id you used in your datasource WHERE configuration is nowhere to be seen in the rendered search page when you use a master page.  Well, nowhere to be seen except as part of a much longer id name that's been generated. :)

    Sunday, January 23, 2011 5:14 AM
  • This problem has been (finally) addressed in ASP.NET 4.0 with the new ClientIDMode. I haven't actually worked that much with 4.0, but I did read about it in an extensive "What's New in 4.0" article. You can find out more by googling "ClientIDMode."

    Of course, support for 4.0 will have to be available, and it's new enough that many providers don't yet have it. Still...  ;-)

    cheers,
    scott


    Please remember to "Mark as Answer" the responses that resolved your issue. It is common courtesy to recognize those who have helped you, and it also makes it easier for visitors to find the resolution later.
    Sunday, January 23, 2011 5:45 AM
  • Believe it or not, that won't help in this case.  The thing you have to hard code into the search Where for FormField is not the generated id, but the generated name.  [That will work.]  I mis-spoke when I refered to id above.  In a page where a master page is not used, asp.net generates a name (if you haven't specified one), and that name is the same as the id, and the id is whatever you called it.

    When a master page is used, ClientIDMode can make the id static, keeping it the same as what you called it, but the generated name is still the complex concatenation you get if ClientIDMode is not used.

    Sunday, January 23, 2011 6:23 AM
  • Well I am sure you are onto something here Kathy, but how to verify this---

    By the way I have posted the problem on the asp.net forum along with the code pages at

    http://forums.asp.net/p/1645423/4267108.aspx#4267108Posted

    I am targeting asp.net 4.0

    When I use Firefox to view the rendered search results page source I dont see anything useful, like the WHERE clause at all. Every thing is in a div with class="aspnethidden" which is nothing I put in the code. I havent worked with the looking at source code this way much -- is there a setting I need to change so that I can see something useful? I would like to be able to see this concatenated ID you are referring to.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Sunday, January 23, 2011 2:01 PM
  • Clark, of course you don't see the WHERE clause in the rendered page.

    All you see in the rendered page is what is sent to the browser, after asp.net does its thing.

    Your textbox name and id are there in the search page - scroll down a bit.

    To verify it, just do what I said:  hard code the rendered textbox "name" (note I corrected myself as I mis-spoke and said "id" in the first reply) from the search page into the Formfield parameter for your WHERE clause in Code View of your results page.  It will now work.

    • Proposed as answer by ClarkNK Monday, January 24, 2011 3:45 AM
    • Marked as answer by cduchon Monday, January 24, 2011 6:39 PM
    Sunday, January 23, 2011 2:38 PM
  • Kathy & Clark,

    Thanks for your help with this... Kathy is correct. I changed the WHERE clause to use the rendered form name

    The original field was “Case” and the rendered name, in my code, is ctl00$ContentPlaceHolder1$Case

    For anyone else that finds has the same problem I found that rendered name in the source of the query page.

    Chuck

     

    Monday, January 24, 2011 1:25 AM
  • Yes, Kathy had it correct. That girl knows what she is doin' !

    Chuck, you got there before I did because I had to go watch some football games :=)

    It's been interesting that's for sure. I had not run into that even though I use Master Pages because I pretty much never put the search results on a different page, I like to use the same page and set Panel Visibilities according to whether I want to show the search form or search results.

    Thanks Kathy.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Monday, January 24, 2011 3:43 AM
  • And, to elaborate a bit, hard coding the rendered name is one way, but not a very robust way if you ever change something (or if asp.net ever changes how it names its rendered controls when using a master page).  Consider it the simplest boot strap way.  Code behind to gather that info and send it to the new page (for example as part of a query string), is more robust.
    Monday, January 24, 2011 5:03 PM
  • Yes Kathy. So I am fiddling with getting one of those more robust ways to work.

    First, which seemed to me most direct, was try passing the info as a session variable, then on page load of the results page assign the session value to an invisible textbox on the results page. The Results page WHERE clause could then easily be conditioned on that textbox value, being that it is right there on the same page.

    The second approach I am trying is to use a previouspage.findcontrol type statement and casting that into the textbox value.

    At first blush, what I think should work does not (probably because of some of the same type issues with asp.net), but over the next few days I hope to get enough time to get one of  those robust methods working. When I do maybe I will document it in the related tutorial, since the problem is so "unexpected". I mean, it sure suprised me!!



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    Monday, January 24, 2011 8:02 PM
  • OK I think I have a robust way of making this work with a Master Page

    The problem is that we have a textbox (I give it ID of  TxtBxHSLName ) inside the contentplacehoder1 of a MasterPage (which is where all the controls of child pages will be) and we want to access the value of that textbox in the WHERE clause when configuring a Gridview datasource on a different page.

    The solution that works for me is:

    1. On the page where your gridview is, put a textbox with the same ID as on the search page: TxtBxHSLName

    2. Then set your gridview as usual, configure its datasource as usual, and set up the WHERE to depend on the value of  TxtBxHSLName

    3. The last step you need some code to run when the Results page loads that assigns the value to TxtBxHSLName that is the same as was entered in that textbox on the search page. If you use VB, that code looks like:

    -----------

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.PreviousPage Is Nothing Then
                Dim placeHolder As Control =
                    PreviousPage.Controls(0).FindControl("ContentPlaceHolder1")
                Dim SourceTextBox As TextBox =
                    CType(placeHolder.FindControl("TxtBxHSLName"), TextBox)
                If Not SourceTextBox Is Nothing Then
                    TxtBxHSLName.Text = SourceTextBox.Text
                End If
            End If

        End Sub

    -------------

    This code digs into the previous page with a find.control to get the value of the textbox on that page and assign that to the textbox on the results page. So I think it will be pretty robust.



    ClarkNK, A.K.A. HomePage Doctor
    HomePageDoctor.com -- Expression Web database tutorials
    Ownertrades.com -- Created with FP, Access, Bots and Wizards
    LawOfAllTheLand.org -- Created with Expression, VWDExress, SQL Express, and ASP.NET.
    Arvixe -- My favored web host
    • Proposed as answer by ClarkNK Tuesday, January 25, 2011 1:20 PM
    • Marked as answer by cduchon Wednesday, January 26, 2011 12:32 AM
    Monday, January 24, 2011 9:21 PM