传递gridview到另一个ASP.net页面

本文关键字:net 页面 ASP 另一个 gridview 传递 | 更新日期: 2023-09-27 18:11:49

我有一个页面,我在其中使用搜索,我想通过网格视图到点击按钮后的结果页?这个按钮是搜索按钮,这个代码在default.aspx.cs中工作,没有问题,但是当我删除网格并将其放入结果中时。Aspx我有一个错误在最后3行。default.aspx.cs

    protected void Button1_Click(object sender, EventArgs e)
    {
        string searchby;
        searchby = DropDownList1.SelectedItem.Text;
        string str = string.Empty;
    switch (searchby)        {
        case "Search By":
            str = "select * from UserInfo u inner join TeamDB T on U.EMC_ID = T.EMC_ID where u.Name like '%' + @search + '%'";
                break;
        case "EMC ID":
            int searchID = int.Parse(TextBox1.Text);
            str = "select U.EMC_ID, Name, Email, CiscoPhone ,T.Department ,T.TeamName , T.ManagerName, T.ManagerID from UserInfo U inner join TeamDB T on U.EMC_ID = T.EMC_ID where U.EMC_ID =" + searchID;
            break;
        case "Name":
            str = "select EMC_ID, Name, Email, CiscoPhone from UserInfo where Name like '%' + @search + '%'";
            break;
        case "Email":
            str = "select EMC_ID, Name, Email, CiscoPhone from UserInfo where Email like '%' + @search + '%'";
            break;
        case "Provider":
            str = "select EMC_ID, Name, Email, CiscoPhone, Provider ,DataSIM, DataLineNumber, Data_IMEA from UserInfo where Provider like '%' + @search + '%'";
            break;
        case "USB Data Sim":
            Int64 searchSIM = Int64.Parse(TextBox1.Text);
            str = "select EMC_ID, Name, Email, CiscoPhone, Provider ,DataSIM, DataLineNumber, Data_IMEA from UserInfo where DataSIM  =" + searchSIM;
            break;
        case "USB Data line Number":
            Int64 searchLine = Int64.Parse(TextBox1.Text);
            str = "select EMC_ID, Name, Email, CiscoPhone, Provider ,DataSIM, DataLineNumber, Data_IMEA from UserInfo where DataLineNumber  =" + searchLine;
            break;
        case "USB Data IMEA":
            Int64 searchIMEA = Int64.Parse(TextBox1.Text);
            str = "select EMC_ID, Name, Email, CiscoPhone, Provider ,DataSIM, DataLineNumber, Data_IMEA from UserInfo where Data_IMEA  =" + searchIMEA;
            break;
        case "Cisco Phone":
            int searchcisco = int.Parse(TextBox1.Text);
            str = "select EMC_ID, Name, Email, CiscoPhone from UserInfo where CiscoPhone =" + searchcisco;
            break;
        case "Team Name":
            str = "select U.EMC_ID, U.Name, T.TeamName, T.ManagerID, T.ManagerName from UserInfo u inner join TeamDB T on U.EMC_ID = T.EMC_ID where T.TeamName like '%' + @search + '%'";
            break;
        default:
            Console.WriteLine("Please make a selection!");  
            break;
    }
        SqlCommand xp = new SqlCommand(str, did);
        xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text; 
        did.Open();
        xp.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = xp;
        DataSet ds = new DataSet();
        da.Fill(ds, "EMC_ID");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        GridView1.Visible = true;
    }

我想重定向此代码以在新页面中打开,该页面为Results.aspx

传递gridview到另一个ASP.net页面

您希望实现搜索,并将结果显示在搜索页面上?解决方案不是传递GridView(这没有意义,因为它是一个UI元素),而是传递搜索查询,并在结果页面上执行搜索。它最终应该是这样的:

搜索页面

protected void SearchBtn_Click(object sender, EventArgs e)
{
    Response.Redirect(String.Format("~/Results.aspx?search={0}",
        Server.UrlEncode(SearchTB.Text));
}
搜索结果页面

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        //run the query, bind results to GridView
    }
}

如果您想要一个更好的用户界面,您的搜索和结果显示都应该在一个页面上完成,并且您应该使用AJAX将搜索条件发送到结果并获得它们。