うまくいけば、これはそこにいる専門家にとって比較的簡単な質問です!
5つのドロップダウンリストがあるUpdateGridView関数があります。これらの5つのリストは同じDataTableにバインドされますが、エンドユーザー(クライアント)は同じSelectedValueを複数回指定することはできません。Group01はRequiredFieldValidatorでカバーされているため、少なくとも1つのグループを指定する必要があります。
私が抱えている問題は、CustomValidation用に作成したものが、selectedValueを比較して、それらが同じでないことを確認するブール値であるということです。ただし、現在のコンテキストには存在しないため、「ddGroup01」または「ddGroup02」と直接言うことはできません。
だから私の質問、サーバー検証のためにドロップダウンリストを存在させるために何をする必要がありますか?
aspxページ。
<asp:GridView ID="gvEditUser" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="gvEditUser_RowDataBound" OnRowCancelingEdit="gvEditUser_RowCancelingEdit" OnRowEditing="gvEditUser_RowEditing" OnRowUpdating="gvEditUser_RowUpdating" OnRowDeleting="gvEditUser_RowDeleting" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="User ID">
<ItemTemplate>
<asp:Label ID="lbUserID" runat="server" Text='<%# Eval("userid") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:Label ID="lbFullName" runat="server" Text='<%# Eval("fullname") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbFullName" runat="server" Text='<%# Eval("fullname") %>' ></asp:TextBox>
<asp:RegularExpressionValidator ID="val_reg_FullName" runat="server" ErrorMessage="Not a valid Full Name." ControlToValidate="tbFullName" ValidationExpression="^[A-Z][a-zA-Z']+[ ]+[A-Z][a-zA-Z'\- ]*$" CssClass="password_red_fail" Display="None"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="valFullName" runat="server" ErrorMessage="Full Name is required." CssClass="password_red_fail" ControlToValidate="tbFullName" Display="None"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lbEmail" runat="server" Text='<%# Eval("email") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group 01">
<ItemTemplate>
<asp:Label ID="lbGroup01" runat="server" Text='<%# Eval("group01") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddGroup01" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="valddGroup01" runat="server" ErrorMessage="Group 01 is required." CssClass="password_red_fail" ControlToValidate="ddGroup01" Display="None"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group 02">
<ItemTemplate>
<asp:Label ID="lbGroup02" runat="server" Text='<%# Eval("group02") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddGroup02" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group 03">
<ItemTemplate>
<asp:Label ID="lbGroup03" runat="server" Text='<%# Eval("group03") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddGroup03" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group 04">
<ItemTemplate>
<asp:Label ID="lbGroup04" runat="server" Text='<%# Eval("group04") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddGroup04" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group 05">
<ItemTemplate>
<asp:Label ID="lbGrou05" runat="server" Text='<%# Eval("group05") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddGroup05" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Admin">
<ItemTemplate>
<asp:CheckBox ID="cbAdmin" runat="server" Checked='<%# Convert.ToBoolean(Eval("admin")) %>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="cbAdmin" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created by">
<ItemTemplate>
<asp:Label ID="lbCreatedBy" runat="server" Text='<%# Eval("createdby") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Login">
<ItemTemplate>
<asp:Label ID="lbLastLogin" runat="server" Text='<%# Eval("lastlogin") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="Black" />
<HeaderStyle BackColor="#507CD1" ForeColor="White" CssClass="size14_text" />
<PagerStyle BackColor="#2461BF" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:ValidationSummary ID="valSummary" runat="server" CssClass="password_red_fail" />
背後にあるコードのスニペット。これが必要な部品であると仮定します。
protected void gvEditUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState != null)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddGroup01 = (DropDownList)e.Row.FindControl("ddGroup01");
ddGroup01.DataTextField = "association";
ddGroup01.DataValueField = "association";
ddGroup01.DataSource = load_group();
ddGroup01.DataBind();
ddGroup01.Items.Insert(0, new ListItem());
DropDownList ddGroup02 = (DropDownList)e.Row.FindControl("ddGroup02");
ddGroup02.DataTextField = "association";
ddGroup02.DataValueField = "association";
ddGroup02.DataSource = load_group();
ddGroup02.DataBind();
ddGroup02.Items.Insert(0, new ListItem());
DropDownList ddGroup03 = (DropDownList)e.Row.FindControl("ddGroup03");
ddGroup03.DataTextField = "association";
ddGroup03.DataValueField = "association";
ddGroup03.DataSource = load_group();
ddGroup03.DataBind();
ddGroup03.Items.Insert(0, new ListItem());
DropDownList ddGroup04 = (DropDownList)e.Row.FindControl("ddGroup04");
ddGroup04.DataTextField = "association";
ddGroup04.DataValueField = "association";
ddGroup04.DataSource = load_group();
ddGroup04.DataBind();
ddGroup04.Items.Insert(0, new ListItem());
DropDownList ddGroup05 = (DropDownList)e.Row.FindControl("ddGroup05");
ddGroup05.DataTextField = "association";
ddGroup05.DataValueField = "association";
ddGroup05.DataSource = load_group();
ddGroup05.DataBind();
ddGroup05.Items.Insert(0, new ListItem());
ddGroup01.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group01").ToString();
ddGroup02.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group02").ToString();
ddGroup03.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group03").ToString();
ddGroup04.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group04").ToString();
ddGroup05.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group05").ToString();
CheckBox cbAdmin = (CheckBox)e.Row.FindControl("cbAdmin");
DataRowView dr = e.Row.DataItem as DataRowView;
if (Convert.ToBoolean(dr["admin"] = true))
{
cbAdmin.Checked = true;
}
else
{
cbAdmin.Checked = false;
}
}
}
}
}
protected void val_sameDropDown_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = !haveSameValue(ddGroup01.SelectedValue, ddGroup02.SelectedValue) &&
!haveSameValue(ddGroup01.SelectedValue, ddGroup03.SelectedValue) &&
!haveSameValue(ddGroup01.SelectedValue, ddGroup04.SelectedValue) &&
!haveSameValue(ddGroup01.SelectedValue, ddGroup05.SelectedValue) &&
!haveSameValue(ddGroup02.SelectedValue, ddGroup03.SelectedValue) &&
!haveSameValue(ddGroup02.SelectedValue, ddGroup04.SelectedValue) &&
!haveSameValue(ddGroup02.SelectedValue, ddGroup05.SelectedValue) &&
!haveSameValue(ddGroup03.SelectedValue, ddGroup04.SelectedValue) &&
!haveSameValue(ddGroup03.SelectedValue, ddGroup05.SelectedValue) &&
!haveSameValue(ddGroup04.SelectedValue, ddGroup05.SelectedValue);
}
protected bool haveSameValue(string first, string second)
{
if (!string.IsNullOrEmpty(first) & !string.IsNullOrEmpty(second) && first.Equals(second))
{
return first.Equals(second);
}
return first == null && second == null;
}
みんなとギャルに感謝します。応答を楽しみにしています(このことを学ぶのが大好きです)
乾杯、
トレント