2016-12-02 16 views
0

このGridviewは、ユーザーが「編集」ボタンをクリックするとそのボタンと削除ボタンが別のものに置き換えられます2つのボタン:「確認」と「取消」、「=可視偽」ですが、唯一、彼らはそれをクリックした行のためここに私のコードと試み、私が作ったの一部です:。C#/ ASPグリッドビューの1行のボタンの状態を更新します

GridViewの:

<asp:GridView ID="GridViewTotal" runat="server" CssClass="list-group-item table-condensed table-hover table-responsive" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="false" DataKeyNames="Id" DataSourceID="SqlDSAdminTable" OnRowCommand="GridViewTotal_RowCommand" OnRowUpdating="GridViewTotal_RowUpdating"> 
         <Columns> 
          <asp:TemplateField ShowHeader="false"> 
           <ItemTemplate> 
            <asp:Button ID="ButtonEdit" runat="server" CssClass="btn btn-success" CausesValidation="false" CommandName="EditData" 
             Text="Edit" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>" Width="100px" Visible="true" /> 
            <asp:Button ID="ButtonDelete" runat="server" CssClass="btn btn-danger" CausesValidation="false" CommandName="DeleteData" 
             Text="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>" Width="100px" OnClientClick="return confirm('Are you sure you want to delete the user?');" Visible="true" /> 
            <asp:Button ID="ButtonConfirmEdit" runat="server" CssClass="btn btn-primary" CausesValidation="false" CommandName="" 
             Text="Confirm" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>" Width="100px" OnClientClick="return confirm('Are you sure you want to edit the user data?');" Visible="false" /> 
            <asp:Button ID="ButtonCancelEdit" runat="server" CssClass="btn btn-danger" CausesValidation="false" CommandName="" 
             Text="Cancel" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>" Width="100px" Visible="false" /> 
           </ItemTemplate> 
          </asp:TemplateField> 

バックエンド:

protected void GridViewTotal_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "EditData") 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      GridViewTotal.EditIndex = index; 
      GridViewTotal_UpdateEditButtons(index, "edit"); 
      GridViewTotal.DataBind(); 


     } 
    } 

UpdateEditButtons():あなたが尋ねる前に

protected void GridViewTotal_UpdateEditButtons(int index, string arg) 
    { 
     Button btnConfirm = (Button)GridViewTotal.Rows[index].FindControl("ButtonConfirmEdit"); 
     Button btnCancel = (Button)GridViewTotal.Rows[index].FindControl("ButtonCancelEdit"); 
     Button btnEdit = (Button)GridViewTotal.Rows[index].FindControl("ButtonEdit"); 
     Button btnDelete = (Button)GridViewTotal.Rows[index].FindControl("ButtonDelete"); 

     if(arg == "edit") 
     { 
      btnDelete.Visible = false; 
      btnEdit.Visible = false; 
      btnConfirm.Visible = true; 
      btnCancel.Visible = true; 
     } 
     else if(arg == "confirm") 
     { 
      btnDelete.Visible = true; 
      btnEdit.Visible = true; 
      btnConfirm.Visible = false; 
      btnCancel.Visible = false; 
     } 
    } 

は、はい、GridViewのは、更新パネルの内側にあり、かつ意図したようにさわやかに取り組んでいます。

ありがとうございます。

答えて

0

[OK]を、RowDataBoundイベントを使用して、解決策を見つけた:私は、関数が置き換えすべきか否かを確認するために、状態変数としてカウンタ変数を使用してい

protected void GridViewTotal_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (_counter == 1) 
      { 
       Button btnEdit = e.Row.FindControl("ButtonEdit") as Button; 
       Button btnDelete = e.Row.FindControl("ButtonDelete") as Button; 
       Button btnConfirm = e.Row.FindControl("ButtonConfirmEdit") as Button; 
       Button btnCancel = e.Row.FindControl("ButtonCancelEdit") as Button; 

       btnCancel.Visible = true; 
       btnConfirm.Visible = true; 
       btnDelete.Visible = false; 
       btnEdit.Visible = false; 
      } 
     } 
    } 

関連する問題