2011-04-02 105 views
0

大家好,我的母版頁上我有一個區域做了搜索,但搜索代碼是另一頁(內容頁面)稱爲Search.aspx發送一個字符串到另一個頁面

就當我按一下按鈕做我的數據庫的搜索查詢我需要從我的母版頁發送字符串到我的搜索頁面並重定向。

Search.aspx代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    private void PopulateWallPosts(string search) 
    { 
     //my search query code 
    } 

母版頁:

protected void Button2_Click(object sender, EventArgs e) 
{ 


    string search = TextBox2.Text; 

    PopulateWallPosts(search); 
    // this method works fine on local page 
    // how do I send this to Search.aspx so when button is clicked im redirected to search.aspx and the content of search populates my wallposts? 

    } 
} 

回答

2

Search.aspx

protected void Page_Load(object sender, EventArgs e) 
{ 
    string search = Request.QueryString["search"]; 
    if (!string.IsNullOrEmpty(search)) 
    { 
     PopulateWallPosts(search); 
    } 
} 

private void PopulateWallPosts(string search) 
{ 
    //my search query code 
} 

母版頁:

protected void Button2_Click(object sender, EventArgs e) 
{ 
    string search = TextBox2.Text; 

    Response.Redirect("Search.aspx?search=" + Server.UrlEncode(search)); 
} 
0

您可以設置按鈕/鏈接的PostBackUrl屬性:

<asp:Button PostBackUrl="Search.aspx" ID="search" runat="server" Text="Search" /> 

而且在Search.aspx頁面從請求中獲取值。例如,如果文本框是在母版頁,被稱爲txtSearch

public partial class Search : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var txtSearch = (TextBox)PreviousPage.Master.FindControl("txtSearch"); 
     if (txtSearch != null) 
     { 
      var value = txtSearch.Text; 
      ... 
     } 
    } 
} 
+0

我如何獲取價值? – 2011-04-02 14:36:01

+0

@Garrith格雷厄姆​​,文本框在哪裏?在母版頁中?如果是的話,Search.aspx從相同的主人派生? – 2011-04-02 14:39:16

+0

yes and atm它的剛纔在主頁面上名爲TextBox1.Text – 2011-04-02 14:48:16

0

只要有搜索框重定向到搜索頁面,傳遞參數的查詢字符串。

相關問題