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



我希望将母版页搜索结果返回到另一个页面上的 GridView,该页面上还有一个搜索按钮。我将不胜感激任何有关如何执行此操作的指示....从搜索页面.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" />

在 Button1 Click 事件中,您可以在 QueryString 中设置 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  + "%'";

最新更新