user1877542
user1877542

Reputation: 11

Retrieving MasterPage textbox Control on a postback to another page

I have spend two days trying to figure out the solution to this problem, even tried ExpertExchange and still I can't get a solution. I am a very novice programmer to ASP.Net (using C#) and I DON'T want to use a string/url post

I have a MasterPage of which has a textbox called tbSearchString. It is a simple box that a user can enter something and then it does a Postback to another page SearchResults.aspx So I also have other pages, like Default.aspx that uses the MasterPage.

I have tried nearly everything and have read nearly every post I could find on the net and no mater what the Variables are always Null.

I have use this code on the searchResults loadpage event and Every one of these variables are null, even though I enter a value in the page text box and click the button to postback to the SearchResults page, the only time it works is if I am on the searchResults page and submit.

SearchResults back end page

protected void Page_Load(object sender, EventArgs e)
{

    TextBox SearchString;
    TextBox SearchString2;
    TextBox SearchString3;
    TextBox SearchString5;   

    if (Page.PreviousPage != null)  //This is true on every test
    {

        SearchString = (TextBox)Page.PreviousPage.Master.FindControl("tbSearchString");
        SearchString2 = (TextBox)PreviousPage.Master.FindControl("tbSearchString");
        SearchString3 = (TextBox)Master.FindControl("tbSearchString");
        TextBox LoginControlx = (TextBox)PreviousPage.FindControl("Form1");

       if (LoginControlx != null)
        {
          TextBox SearchString4 = (TextBox)LoginControlx.FindControl("tbSearchString");
        }
  }

MainWebsite.Master page Code

<asp:TextBox ID="tbSearchString" runat="server"></asp:TextBox>
                <asp:Button ID="btnSearch1" runat="server" Text="Search" PostBackUrl="~/SearchResultRentalEquiptment.aspx" />

I don't have anything in the CS backend page

So on the Default.aspx page

nothing special Just the Masterpage and some text content, I enter some text in the textbox goes to the SearchResults page and I can not get the darn value from the Textbox control from the Default or any other page.

What say you wise ones?

Upvotes: 0

Views: 1946

Answers (3)

EternityZ
EternityZ

Reputation: 11

how do you redirect your form to search result form? if you are using Response.redirect, the value under Page.PreviousPage.Master.FindControl will be null . Try to use Server.Transfer to see if it works.

Upvotes: 1

CptSupermrkt
CptSupermrkt

Reputation: 7124

Check out Session. I use it all the time when I need to get data from one page to another. I'm not currently able to write out a full example for you, but off the top of my head the following should work:

//page1.aspx:

protected void btnSubmit_Click(object sender, EventArgs e)
{
     string greetingString = "Hello";
     Session["MyValue"] = greetingString;
     Response.Redirect("page2.aspx");
}

//page2.aspx:

protected void Page_Load(object sender, EventArgs e)
{
     Response.Write(Session["MyValue"].ToString());  //prints "Hello"
}

Upvotes: 0

EdSF
EdSF

Reputation: 12341

Here's one way:

Assuming this is your Master Page code:

<asp:TextBox ID="searchbox" runat="server" /><br />
<asp:Button ID="sendSearch" runat="server" PostBackUrl="~/Results.aspx" Text="Search" />

At the end of the day, it's all about HTTP POST, so in the target page Results.aspx PageLoad:

protected void Page_Load(object sender, EventArgs e)
{
    string _foo = Request.Form[this.Master.FindControl("searchbox").UniqueID];
}

Hth....

Upvotes: 0

Related Questions