【发布时间】: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