【问题标题】:ASP.NET - Get control in RowCommand eventASP.NET - 在 RowCommand 事件中获取控制权
【发布时间】:2012-02-18 23:55:09
【问题描述】:

尝试在 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。我的问题是这个;页面加载时,其中包含所有记录,包括底部的空行(页脚)。如果我在填充的行上单击编辑,数据会正确填充,然后当我点击更新时,我会收到来自 FOOTER RFV 触发的所有错误消息,这是不正确的。因此,在 RowCommand 事件中,我想尝试这样做:如果用户单击 EDIT 按钮,则禁用页脚行(添加新行)中的所有 RFV,如果他们单击其他任何内容,则启用它们。

想法?

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

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

【问题讨论】:

  • 空行有什么用 - 你真的需要它吗?
  • 是的,空行用于插入新行。

标签: asp.net


【解决方案1】:

您应该在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,则需要将适当的ValidationGroup 添加到每个ValidationSummary。如果将此属性留空,则只会列出没有指定 ValidationGroup 的控件。

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

【讨论】:

  • 谢谢,这是完美的。还应该注意的是,如果您使用的是 ValidationSummary 控件(就像我一样),那么您还需要设置 ValidationGroup 属性。
  • 哦,您需要为您创建的每个 ValidationGroup 提供一个 ValidationSummary;除非我在那里遗漏了什么。
  • @Robert:是的,这是一个错字,感谢您的编辑。你是对的,你需要为每个 ValidationGroup 单独的 ValidationSummary(编辑我的答案)。 msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 2011-09-24
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
相关资源
最近更新 更多