Results 1 to 8 of 8

Thread: [RESOLVED] Master page postback

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [RESOLVED] Master page postback

    Hi guys i have a question, i'm try to get the value of a textbox control that placed inside a master page, i used this code for retrieving back the texbox values:

    Code:
     If Not Page.PreviousPage Is Nothing Then
                search = TryCast(Master.Page.PreviousPage.FindControl("SearchPost"), TextBox)
                If Not search Is Nothing Then
                    Response.Write("My Text: " & search.Text)
                End If
            Else
                search = TryCast(Master.FindControl("SearchPost"), TextBox)
    
            End If
    Now, if I try to get the textbox value where i'm in the target page (the page that contain that cod) there is no problem.

    but if i'm posting it from Page A to Page B (target page) i can't get the master page textbox control value.

    any suggestions ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Master page postback

    Cast Page.PreviousPage to its actual type. (If previous page is Page9.aspx, then cast it to type Page9)

    Then, look at its Master property and cast that to the type. So if Page9's Master page is called MasterPencil.master, then cast it to type MasterPencil. Then do a FindControl and look for the textbox.

  3. #3

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Master page postback

    sorry for my ignorant, but how do i cast Page.PreviousPage to its original page (code wise) , i kind of new to asp.net
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  4. #4
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Master page postback

    Something like
    Code:
    Dim test As Page = DirectCast((Page.PreviousPage), Page9) --There should be a page in your project called Page9.aspx
    Dim previousMaster As MasterPage = DirectCast((test.Master), MasterPage)

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Master page postback

    The name of that Page is "Default" and that seems to be a preserved word, what should i do now?

    and i really don't understand why simple parameters posting needs to be so hard, isn't asp.net suppose to make life easier?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Master page postback

    I didn't realize that could be an issue and I posted the code above just thinking about how to do a cast.

    As for simplicity, the simplest way to pass values across pages are QueryString, Session (should be judiciously used), class properties etc. If you are trying to do a cross page posting with master pages I would suggest reading this article

  7. #7

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Master page postback

    Thank you for your help rjv,
    I made a small adjustment to the code you provide me and it work perfectly when posting from every page
    Code:
      Dim test As Page = DirectCast((Page.PreviousPage), Page)
                Dim previousMaster As MasterPage = DirectCast((test.Master), MasterPage)
    i changed the "Page9" to "Page" and now i can retrieve the value of the master page text box control from any page.

    I will now read the article you provided and hopfully i when i done i'll be a little bit smarter in this subject.

    Thanks.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Master page postback

    It doesn't matter what the page is named, you simply need the class type. You'd get that from the codebehind, right at the top, at the class declaration.

    But yes, you're getting yourself into an unnecessarily complicated situation; think about the purpose of this textbox, whether its value needs to be used throughout the app, and if it does, how you should be passing it around.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width