在RowUpdating(ASP.NET GridView)C#中获取GridView中的旧标签值

时间:2017-04-26 12:53:00

标签: c# asp.net gridview

我一直在努力寻找一种方法来获得一个“老人”#34;我的GridView中的标签值。我尝试过使用hiddenFields保存它们(这会给我这个错误) Error when using hiddenFields

我也试过使用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>

我的Codebehind:

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();
}

1 个答案:

答案 0 :(得分: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];