将字符串发送到另一页

时间:2011-04-02 14:27:54

标签: c# asp.net html

您好,在我的母版页上,我有一个区域可以进行搜索,但我的搜索代码位于另一个名为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?

    }
}

3 个答案:

答案 0 :(得分: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));
}

答案 1 :(得分: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;
            ...
        }
    }
}

答案 2 :(得分:0)

只需将搜索框重定向到搜索页面,然后在查询字符串上传递参数即可。