我需要在我的代码中设置ValidationGroup名称。我有一个嵌套网格,如果我在标记中设置ValidationGroup名称,那么当我点击“添加”#39;按钮来验证条目,它检查第一个嵌套网格,如果有效则成功,否则失败。 所以在我的RowDataBound事件中,我为每个嵌套网格设置ValidationGroups的值,如下所示:
protected void UserGroupGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int tiGroupID = Convert.ToInt32(UserGroupGridView.DataKeys[e.Row.RowIndex].Value.ToString());
// m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);
GridView tgvGroupMember = (GridView)e.Row.FindControl("GroupMemberGridView");
populateGroupMemberGrid(tgvGroupMember, tiGroupID);
//Get the Footer controls that have the new entry data
Control tFooterControls = CommonMethods.getFooterControls(tgvGroupMember);
DropDownList tddlRecipientNames = tFooterControls.FindControl("mggvDDLMemberName") as DropDownList;
m_strXmlTableData = m_pagingClient.GetAllPossibleGroupMembers(m_strUserID, tiGroupID);
DataTable tdtAllGroupMembers = CommonMethods.ParseXML(m_strXmlTableData);
tddlRecipientNames.DataSource = tdtAllGroupMembers;
tddlRecipientNames.DataTextField = tdtAllGroupMembers.Columns["MemberName"].ToString();
tddlRecipientNames.DataValueField = tdtAllGroupMembers.Columns["MemberID"].ToString();
tddlRecipientNames.DataBind();
tddlRecipientNames.Items.Insert(0, new ListItem("", "0"));//This is needed for the jquery-chosen dropdown to add data-holder text
tddlRecipientNames.ValidationGroup = "DDLVal_" + tiGroupID.ToString();
Button tbtnAdd = tFooterControls.FindControl("mggvAddButton") as Button;
tbtnAdd.ValidationGroup = "DDLVal_" + tiGroupID.ToString();
}
}
catch (Exception ex)
{
//TO DO: Response.Redirect("~/Error.aspx");
}
}
但是,当我检查jquery函数中的所有Page_Validators时,两者都未定义'。这是jquery函数:
function Validate(sender) {
for (i = 0; i < Page_Validators.length; i++) {
alert(Page_Validators[i].validationGroup);
}
}
我在哪里可以在后面的代码中设置我的ValidationGroup名称,以便我可以在客户端使用它们。 为完整起见,这是嵌套网格标记:
<div id="div<%# Eval("GroupID") %>" style="display:none">
<asp:GridView ID="GroupMemberGridView" runat="server" AutoGenerateColumns="false" OnRowDeleting="GroupMemberGridView_RowDeleting"
OnRowCommand="GroupMemberGridView_RowCommand" CssClass="grid" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="MemberID">
<ItemTemplate>
<asp:Label ID="mggvLblMemberID" runat="server" Text='<%# Bind("MemberID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="mggvLblMemberName" runat="server" Text='<%# Bind("MemberName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="mggvDDLMemberName" runat="server" ClientIDMode="Static"
class="chosen-single" data-placeholder="Choose member…">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqValueDDLMemberInsert" runat="server" InitialValue="0"
ControlToValidate="mggvDDLMemberName"
ErrorMessage="Selection required." CssClass="message-error-dropdown">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active Device(s)">
<ItemTemplate>
<asp:Label ID="mggvLblActiveDevice" runat="server" Text='<%# (Boolean.Parse(Eval("ActiveDevice").ToString())) ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="mggvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" CssClass="gridActionbutton" OnClientClick="return confirm('Are you sure you want to delete this Group Member?')" >
</asp:Button>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="mggvAddButton" runat="server" CommandName="Add" Text="Add Member" Width="90%" ClientIDMode="Static"
CssClass="gridActionbutton" CausesValidation="true" OnClientClick="return Validate(this);" >
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>