0

我希望使母版页搜索将结果返回到另一个页面上的 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" />
4

1 回答 1

1

在 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  + "%'";
于 2012-12-10T14:36:50.020 回答