2011-06-22 14 views
0

検索ボタンをクリックするとGridView.Atを読み込み、追加、削除、更新できます。ユーザーが追加をクリックすると、メッセージボックスが表示され、「お客様は正常に追加されました」 「SearchButton」を再度クリックする必要があります。再度GridViewを表示できません。 原因私は、SearchボタンをクリックしてGridviewをロードするコードを作成しています。どうすれば修正できますか? 初めての場合、ドロップダウンリストと検索ボタンのみがあります。私を助けてください。おかげ検索ボタンload gridview after c#asp.net 2.0

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>"); 

        } 
     } 

答えて

0
  1. は、グリッドビューが示さかないことがどうかを決定するためにビューステート内のフラグを持っています。初期値はfalseです。
  2. ShowGridというグリッドを追加(必要な場合)し、データをバインドして表示する方法があります。
  3. 検索のクリックで、ビューステートフラグをtrueに設定し、ShowGridメソッドを呼び出します。
  4. Page_Loadイベントでは、ビューステートフラグがtrue(ポストバック状態)であることを確認し、trueの場合はShowGridメソッドを呼び出します。