2017-04-26 60 views
1

我一直在努力尋找一種方法在GridView中獲取標籤的「舊」值。我已經使用hiddenFields拯救他們試圖(這會給我這個錯誤) Error when using hiddenFields在RowUpdating(ASP.NET GridView)中獲取GridView內部標籤的舊值C#

我使用的EventArgs(e.OldValues)也嘗試過,但那些總是空的(所以是在e.NewValues)

任何人都可以幫我嗎? 我的GridView:

<asp:GridView CssClass="gridview" ID="GridViewAdmin_Users" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" OnPageIndexChanging="GridViewAdmin_Users_PageIndexChanging" OnRowCancelingEdit="GridViewAdmin_Users_RowCancelingEdit" OnRowDeleting="GridViewAdmin_Users_RowDeleting" OnRowEditing="GridViewAdmin_Users_RowEditing" OnRowUpdating="GridViewAdmin_Users_RowUpdating" DataKeyNames="ID" EnableViewState="True"> 
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    <Columns> 
     <asp:TemplateField HeaderText="ID"> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersID" runat="server" Text='<%# Eval("id") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Gebruikersnaam"> 
      <EditItemTemplate> 
       <asp:TextBox ID="txtUserEditNaam" runat="server" Text='<%# Eval("naam") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersName" runat="server" Text='<%# Eval("naam") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Bijnaam"> 
      <EditItemTemplate> 
       <asp:TextBox ID="txtUserEditBijnaam" runat="server" Text='<%# Eval("bijnaam") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersNickname" runat="server" Text='<%# Eval("bijnaam") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Wachtwoord"> 
      <EditItemTemplate> 
       <asp:HiddenField ID="OldPassword" Value='<%# Bind("password") %>' runat="server" /> 
       <asp:TextBox ID="txtUserEditPassword" runat="server" Text='<%# Eval("password") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersWachtwoord" runat="server" Text='<%# Eval("password") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Salt"> 
      <EditItemTemplate> 
       <asp:HiddenField ID="OldSalt" Value='<%# Bind("salt") %>' runat="server" /> 
       <asp:TextBox ID="txtUserEditSalt" runat="server" Text='<%# Eval("salt") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersSalt" runat="server" Text='<%# Eval("salt") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Machtigingen"> 
      <EditItemTemplate> 
       <asp:TextBox ID="txtUserEditPermission" runat="server" Text='<%# Eval("permission") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblUsersPermissions" runat="server" Text='<%# Eval("permission") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="ChangePassword"> 
      <EditItemTemplate> 
       <asp:TextBox ID="txtUserEditChangePW" runat="server" Text='<%# Eval("changepassword") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblChangePassword" runat="server" Text='<%# Eval("changepassword") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True" ButtonType="Button" EditText="Aanpassen" /> 
     <asp:CommandField ShowDeleteButton="True" ButtonType="Button" EditText="Verwijderen" /> 
    </Columns> 
    <EditRowStyle BackColor="#999999" /> 
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
    <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
    <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
</asp:GridView> 

我隱藏:

protected void GridViewAdmin_Users_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    ShowActiveGrid("panelUsers"); 
    int id = Convert.ToInt16(GridViewAdmin_Users.DataKeys[e.RowIndex].Value); 
    // Naam ophalen vanuit de inline edit 
    TextBox txtUserEditNaam = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditNaam") as TextBox; 
    string naam = txtUserEditNaam.Text.Trim(); 
    // Bijnaam ophalen vanuit de inline edit 
    TextBox txtUserEditBijnaam = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditBijnaam") as TextBox; 
    string bijnaam = txtUserEditBijnaam.Text.Trim(); 
    // Password ophalen vanuit de inline edit 
    TextBox txtUserEditPassword = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditPassword") as TextBox; 
    string password = txtUserEditPassword.Text; 
    // Salt ophalen vanuit de inline edit 
    TextBox txtUserEditSalt = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditSalt") as TextBox; 
    string salt = txtUserEditSalt.Text; 
    // Permission ophanel vanuit de inline edit 
    TextBox txtUserEditPermission = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditPermission") as TextBox; 
    string permission = txtUserEditPermission.Text; 
    // ChangePassword ophalen vanuit de inline edit 
    TextBox txtUserEditChangePW = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("txtUserEditChangePW") as TextBox; 
    bool chanepassword = Convert.ToBoolean(txtUserEditChangePW.Text); 

    // Wachtwoorden en salt vergelijken 
    Label lblpassword = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("OldPassword") as Label; 
    string orginial_password = lblpassword.Text; 
    Label lblsalt = GridViewAdmin_Users.Rows[e.RowIndex].FindControl("OldSalt") as Label; 
    string original_salt = lblsalt.Text; 

    // Hier vergelijken we het orginele wachtwoord met het 'nieuwe' wachtwoord. 
    // Als die gelijk zijn betekent dat de gebruiker niets heeft gewijzigd aan het wachtwoord en dat we het wachtwoord kunnen laten zoals het was 
    // Als de gebruiker echter een nieuw wachtwoord heeft ingevoerd dan kunnen we die niet in cleartext laten staan want: 
    // - Dat is onveilig 
    // - Hij zou nooit kunnen inloggen omdat het systeem denkt dat het het wachtwoord moet gehasht worden. 
    if (orginial_password != password) 
    { 
     password = Security.GetHash(Security.GetHashAlgorithm(), password, salt); 
    } 

    if (orginial_password == password && original_salt != salt) 
    { 
     //exception ex = new exception("voer ook het wachtwoord opnieuw in als je de salt wijzigt"); 
     //throw (ex); 
     GridViewAdmin_Users.EditIndex = -1; 
    } 

    // Edit uitvoeren 
    if ((string)Session["permission"] == "WRITE" || (string)Session["permission"] == "*") 
    { 
     controller.EditUser(id, naam, bijnaam, password, permission, chanepassword, salt); 
     // Controlleren of het systeem deze wijziging moet loggen. 
     if (controller.Settings["log_users_edit"]) 
     { 
      controller.Log((int)Session["uid"], "EDIT", (string)Session["username"] + " heeft een user (" + id + ") aangepast."); 
     } 
    } 

    // Grid opnieuw laden 
    GridViewAdmin_Users.EditIndex = -1; 
    Grid(); 
} 

回答

0

您可以在GridView使用DataKeyNames

<asp:GridView ID="GridViewAdmin_Users" runat="server" DataKeyNames="OldPassword, OldSalt"> 

然後您可以訪問後面代碼中的那些鍵值。

string orginial_password = GridViewAdmin_Users.DataKeys[e.RowIndex].Values[0]; 
string original_salt = GridViewAdmin_Users.DataKeys[e.RowIndex].Values[1]; 
相關問題