将网格视图的单元格值与文本框值进行比较

本文关键字:比较 文本 视图 单元格 网格 | 更新日期: 2023-09-27 18:36:36

我有一个 6 列的网格视图 (SAPUserID ,SAPDescription,SAPPassword,OSUserID,OSDescription,OSPassword)和一个CHANGE PASSWORD按钮。单击此按钮后,网格视图下方的面板(pnlChangePwd)变得可见,其中包含3个文本框(User ID, New Password, Confirm Password)和一个Save按钮。目前,如果我输入与旧密码相同的新密码,它接受。如何将txtNewPassword中的值与网格视图中的 SAPPassword 或 OSPassword 的单元格值进行比较?

注意:根据用户所需的数据(SAP或OS),一次只能显示3列

网格视图的代码:

<asp:GridView runat="server" ID="gvPassInfo" AutoGenerateColumns="false" 
                          CellPadding="4" ForeColor="#333333" GridLines="Both" DataKeyNames="user_id" 
                          CssClass="Gridview"  OnRowEditing="gvPassInfo_RowEditing"
                          OnRowCommand="gvPassInfo_RowCommand"  OnRowDataBound="gvPassInfo_RowDataBound">
                <RowStyle BackColor="#EFF3FB" />
                <Columns>                                       
                    <asp:TemplateField HeaderText="User ID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblSAPUserId" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="User ID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblOSUserId" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblSAPDescription" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblOSDescription" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Password" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblSAPPassword" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Password" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblOSPassword" runat="server">
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Change Password">
                        <ItemTemplate>
                            <asp:ImageButton ID="ImgBtnChangePass" runat="server" ImageUrl="~/images/PW.jpg" CausesValidation="false"
                                             CommandName="Edit" />
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

将网格视图的单元格值与文本框值进行比较

这是"保存"按钮的代码

    var currentRow = gvPassInfo.Rows.OfType<GridViewRow>().Single(r => ((Label)r.FindControl("lblSAPUserId")).Text == txtUserId.Text);
    bool doesMatch = ((Label)currentRow.FindControl("lblOSPassword")).Text == "NewPassword";

但是,最好在要绑定到网格的集合中搜索密码。