将gridview传递给另一个页面ASP.net

时间:2015-09-09 13:01:50

标签: c# asp.net gridview

我有一个页面,我在其中使用搜索,我想在点击按钮后将网格视图传递到结果页面? 这个按钮是搜索按钮,这个代码在default.aspx.cs中工作没有问题,但是当我删除网格并将它放在results.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;
    }

我想将此代码重定向到在新页面中打开,即Result.aspx

1 个答案:

答案 0 :(得分:1)

您想要实施搜索,并将结果显示在搜索页面上?然后,解决方案不能通过GridView(因为它无论如何都没有意义),而是传递搜索查询,并在结果页面上执行搜索。最终应该是这样的:

搜索页面

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将搜索词发送到结果并将其取回。