【问题标题】:search button load gridview after c# asp.net 2.0c#asp.net 2.0后搜索按钮加载gridview
【发布时间】:2011-06-22 11:19:21
【问题描述】:

当我单击搜索按钮时,加载 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>");

                    }
      }

【问题讨论】:

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


    【解决方案1】:
    1. 在视图状态中有一个标志来决定是否显示网格视图。初始值为 false。
    2. 有一个方法说ShowGrid,它将添加(如果需要)您的网格、绑定数据并使其可见。
    3. 单击搜索时,将视图状态标志设置为 true 并调用 ShowGrid 方法。
    4. Page_Load 事件中,检查视图状态标志是否为真(在回发条件下),如果为真则调用ShowGrid 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多