母版页上的搜索按钮返回到单独搜索页面上的现有GridView

时间:2012-12-10 14:17:02

标签: c# search master

我正在寻找一个母版页搜索返回结果到另一个页面上的GridView,它还有一个搜索按钮。对于如何做到这一点的任何指示,我将不胜感激.... 来自Search Page .cs文件。以下是“搜索代码”页面及其.cs页面,搜索页面按钮代码和母版页搜索按钮代码:

来自搜索页面.cs文件:

if (IsPostBack)
{
    OdbcConnection MyConnection = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=petsdat; UID=root; PASSWORD=; OPTION=3");
    MyConnection.Open();

    OdbcCommand MyCommand = MyConnection.CreateCommand();
    MyCommand.CommandText = "SELECT * FROM pets WHERE species like '%" + txtSearch.Text + "%'";

    OdbcDataReader MyDataReader = MyCommand.ExecuteReader();

    grdSearch.DataSource = MyDataReader;

    grdSearch.DataBind();

    MyConnection.Close();
}

来自搜索页面:

<form id="form2" >
    <div>
        <h1>Search for Pets</h1>
        <hr />
        <asp:Label runat="server" ID="lblSearch" Text="Search"></asp:Label>
        <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSearch" ErrorMessage="Try Again"></asp:RequiredFieldValidator>

        <asp:Button runat="server" ID="btnSubmit" PostBackUrl="~/Search.aspx" Text="Submit" />
        <br />
          <br />
           <br />
        <asp:GridView runat="server" ID="grdSearch" BorderColor="#CC6600" 
            BorderStyle="Solid" BorderWidth="1px" Font-Names="Arial" Font-Size="Medium" 
            GridLines="Both" HorizontalAlign="Left"   Width="600px"></asp:GridView>
        <br />

        <br />
        <br />
        <br />
        <br />
        <br />
    </div>
    </form>
</asp:Content>

母版页搜索提交按钮代码:

<asp:TextBox ID="Search1" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" PostBackUrl="~/Search.aspx" />

1 个答案:

答案 0 :(得分:1)

在Button1 Click事件上,您可以在QueryString&amp;中设置Search1(TextBox)的值。重定向到您的搜索页面。

例如:

protected void Button1_Click(object sender, EventArgs e)
{
   Response.Redirect("~/Search.aspx?SearchText=" + Search1.Text);        
}

此外,您还必须修改搜索页码:

string searchText = "";
if(Request.QueryString["SearchText"] != null)
  searchText = Request.QueryString["SearchText"];
MyCommand.CommandText = 
          "SELECT * FROM pets WHERE species like '%" + searchText  + "%'";