在RowUpdating(ASP.NET GridView)C#中获取GridView内部标签的旧值 [英] Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#

查看:40
本文介绍了在RowUpdating(ASP.NET GridView)C#中获取GridView内部标签的旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找一种方法来在GridView中获得标签的旧"值.我尝试使用hiddenFields保存它们(这会给我这个错误)使用hiddenFields时出错

I've been struggling to find a way to get an "old" value of a label in my GridView. I've tried using hiddenFields to save them (this would give me this error) Error when using hiddenFields

我也尝试使用EventArgs(e.OldValues),但它们始终为空(e.NewValues也是如此)

I've also tried using the EventArgs (e.OldValues) but those were always empty (So were the e.NewValues)

有人可以帮我吗?我的GridView:

Can anyone help me out? My 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();
}

推荐答案

您可以在GridView中使用 DataKeyNames .

You can use DataKeyNames in the GridView.

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

然后您就可以在后面的代码中访问这些键值.

Then you have access to those key values in code behind.

string orginial_password = GridViewAdmin_Users.DataKeys[e.RowIndex].Values[0];
string original_salt = GridViewAdmin_Users.DataKeys[e.RowIndex].Values[1];

这篇关于在RowUpdating(ASP.NET GridView)C#中获取GridView内部标签的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆