所有。这是我第一篇StackOverflow的帖子。伟大的网站,并经常帮助我,但我找不到答案。
我有一个带有TextBox的asp.net表单,它有一个RequiredFieldValidator。在此之后是启用了编辑和删除的DetailsView。文本框不在DetailsView内。单击“编辑”和/或“删除”命令时,我需要触发RequiredFieldValidator。
这是我的两个控件的代码。我可以用后面的代码发布所有内容但如果不需要则发布很多。
TIA的帮助。
阿里 .................................................. ......................
<asp:TextBox ID="txtReason" runat="server" Width="500px"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvReason" runat="server" ControlToValidate="txtReason"
ErrorMessage="Reason is required for Edit/Delete"></asp:RequiredFieldValidator>
<asp:DetailsView ID="dvCOHDetail" runat="server" Height="50px" Width="700px" DataKeyNames="RecID" DataSourceID="sdsCOHIncident" AutoGenerateRows="False" OnDatabound="dvCOHDetail_DataBound" AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" OnItemUpdated="dvCOHDetail_ItemUpdated" OnItemDeleting="dvCOHDetail_ItemDeleting">
<Fields>
<asp:TemplateField HeaderText="ACTION:">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat=server Text="Edit" CommandName="edit"/>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" CommandName="delete" OnClientClick="return confirm ( 'Are you sure you want to delete this record?' )"/>
</ItemTemplate>
</asp:TemplateField>
............................................... .........................
答案 0 :(得分:0)
下午好,我相信如果你在DetailsView和RequiredFieldValidator中设置了两个按钮的ValidationGroup属性,当点击按钮时,验证器应该激活它的validate事件。 ValidationGroup属性可以是您要描述您尝试验证的验证组的任何字符串。
答案 1 :(得分:0)
您必须对要验证并且验证的所有控件进行分组。如果您要验证 LinkButton 上的 TextBox ,请点击您必须设置 TextBox 的 ValidationGroup 属性想验证和使用相同值(任何字符串)进行验证的 LinkButtons 。
<asp:TextBox ID="txtReason" runat="server" Width="500px"></asp:TextBox>
<asp:DetailsView ID="dvCOHDetail" runat="server" Height="50px" Width="700px" DataKeyNames="RecID" DataSourceID="sdsCOHIncident" AutoGenerateRows="False" OnDatabound="dvCOHDetail_DataBound" AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" OnItemUpdated="dvCOHDetail_ItemUpdated" OnItemDeleting="dvCOHDetail_ItemDeleting">
<Fields>
<asp:TemplateField HeaderText="ACTION:">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" ValidationGroup="ValidationGroup1" Text="Edit" CommandName="edit"/>
<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="ValidationGroup1" Text="Delete" CommandName="delete" OnClientClick="return confirm ( 'Are you sure you want to delete this record?' )"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:RequiredFieldValidator ID="rfvReason" runat="server" ControlToValidate="txtReason"
ErrorMessage="Reason is required for Edit/Delete" ValidationGroup="ValidationGroup1" EnableClientScript="False"></asp:RequiredFieldValidator>