搜索按钮在c#asp.net 2.0之后加载gridview

时间:2011-06-22 11:19:21

标签: c# asp.net gridview .net-2.0

当我单击搜索按钮时,加载Gridview。对于GridView,我可以添加,删除和更新。当用户单击添加时,弹出消息框“客户已成功添加” 然后我必须再次点击“SearchButton”。如果不能再看到GridView。 因为我只是通过单击“搜索”按钮来加载Gridview的代码。我该如何解决? 对于第一次,我只有一些下拉列表和搜索按钮。请帮我。谢谢

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Check if Add button clicked
        if (e.CommandName == "AddProject")
        {
            try
            {
                //Get the values stored in the text boxes


                string strProjectName = ((TextBox)GridView1.FooterRow.FindControl("txtPJDESCLONG")).Text;
                string strLastUpdate = ((TextBox)GridView1.FooterRow.FindControl("txtLASTUPDATE")).Text;
                string strStatus = ((TextBox)GridView1.FooterRow.FindControl("txtSTATUS")).Text;
                string strUsername = ((TextBox)GridView1.FooterRow.FindControl("txtUSERNAME")).Text;
                string strProjCode = ((TextBox)GridView1.FooterRow.FindControl("txtPJCODE")).Text;


               //Prepare the Insert Command of the DataSource control

                DataSet ds = new DataSet();
                SqlConnection conn = new SqlConnection(strConn);
                string strSQL = "";


                 if (conn.State == ConnectionState.Closed) { conn.Open(); }

                 strSQL = "INSERT INTO  CTORGPROJ (CTPAPBRCH,CTPAPDIV,CTPAPDEPT,CTPAPSECT,CTPAPSSEC,CTPAPPDIV,CTPAPLOC,CTPAPPDEP,PJCODE,PJDESCLONG,LASTUPDATE,STATUS, " +
                 "USERNAME)  VALUES ('" + cboBranch.SelectedValue + "','" + cboDivision.SelectedValue + "','" + cboDepartment.SelectedValue + "','" + cboSection.SelectedValue + "','" + cboSubSection.SelectedValue + "','" + cboLocation.SelectedValue + "','" + cboPayDivision.SelectedValue + "','" + cboPayDepartment.SelectedValue + "','" + strProjCode + "','" + strProjectName + "','" +
                 strLastUpdate + "','" + strStatus + "','" + strUsername + "')";

                 Session.Add("conn", CookieUtil.GetTripleDESEncryptedCookieValue("sConn").Replace("-", ";"));
            Session.Add("PjNo", CookieUtil.GetTripleDESEncryptedCookieValue("sPjCode"));
            Session.Add("sComCode", CookieUtil.GetTripleDESEncryptedCookieValue("sComCode"));

            string _strConn = Session["conn"].ToString();
            _strConn = _strConn.Replace("Provider=SQLOLEDB;", "");

            SqlDataSource SqlDataSource1 = new SqlDataSource(_strConn, strSQL);


                        SqlDataSource1.InsertCommand = strSQL;
                        SqlDataSource1.Insert();

                        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Customer added successfully');</script>");

                        GridView1.DataBind();

                        conn.Close();
                        conn.Dispose();

                 }

                    catch (Exception ex)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");

                    }
      }

1 个答案:

答案 0 :(得分:0)

  1. 在视图状态中有一个标志,以决定是否显示网格视图。初始值为false。
  2. 有一个方法说ShowGrid将添加(如果需要)您的网格,绑定数据并使其可见。
  3. 点击搜索,将view-state标志设置为true并调用ShowGrid方法。
  4. Page_Load事件中,检查视图状态标志是否为真(在后置条件下),如果为真,则调用ShowGrid方法。