ASP.NET - 在RowCommand事件中获取控件

时间:2012-02-18 23:55:09

标签: asp.net

尝试访问RowCommand事件中GridView内部的RequiredFieldValidator控件并遇到问题。

这是部分GridView代码:

<asp:TemplateField HeaderText="Password">
    <ItemTemplate>
        <asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password" Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtWebPassword" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvPassword" runat="server" SetFocusOnError="true"
            ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddWebPassword" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvAddPassword" runat="server" SetFocusOnError="true"
            ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
    </FooterTemplate>
</asp:TemplateField>

如您所见,EditTemplate和FooterTemplate有一个RFV。我的问题是这个;当页面加载时,它包含所有记录,包括底部的一个emtpy行(页脚)。如果我在已填充的行上单击“编辑”,则会正确填充数据,然后当我按下UPDATE时,我会从FOOTER RFV的关闭中获取所有错误消息,这是不正确的。因此,在RowCommand事件中,我想尝试这样做:如果用户单击EDIT按钮,则禁用页脚行中的所有RFV(添加新行),如果他们单击其他任何内容,则启用它们。

想法?

抱歉,这是第一次出现这个问题。在RowCommand事件中,我能够找到控件,但是当我将属性设置为虚假的东西时,它似乎被RowDataBound事件稍后覆盖:

            RequiredFieldValidator rfv = (RequiredFieldValidator)gvUsers.FooterRow.FindControl("rfvAddWebLogin");
            rfv.ControlToValidate = string.Empty;
            rfv.ErrorMessage = "sdfgsdfgsdgsdfgsdfgsdfgsdfg";
            rfv.Enabled = false;

1 个答案:

答案 0 :(得分:2)

您应该在EditItemTemplateFooterItemplate中使用不同的ValidationGroups

<asp:TemplateField HeaderText="">
    <ItemTemplate>
        <asp:Button ID="BtnEdit" CausesValidation="False" Text="Edit" CommandName="Edit"  runat="server"  />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:Button ID="BtnUpdate" ValidationGroup="UpdateUser" Text="Update" CommandName="Update"  runat="server" />
    </EditItemTemplate>
    <FooterTemplate>
        <asp:Button ID="BtnInsert" ValidationGroup="InsertUser" Text="Add" CommandName="Insert" runat="server" />
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
    <ItemTemplate>
        <asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password"
            Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtWebPassword" ValidationGroup="UpdateUser" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvPassword" ValidationGroup="UpdateUser" runat="server" SetFocusOnError="true"
            ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtAddWebPassword" ValidationGroup="InsertUser" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvAddPassword" ValidationGroup="InsertUser" runat="server" SetFocusOnError="true"
            ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
    </FooterTemplate>
</asp:TemplateField>

http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic7

注意:如果您使用的是ValidationSummaries,则需要在每个ValidationSummary中添加相应的ValidationGroup。如果将此属性留空,则仅列出没有指定ValidationGroup的控件。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.validationgroup.aspx

相关问题