Hopefully this is a relative quick question for the experts out there!
I have a Update GridView function that has 5 drop down lists. These 5 lists bind to the same DataTable but the end user (client) cannot specify the same SelectedValue more than once. Group01 is covered by a RequiredFieldValidator so they must specify at least one group.
The issue I have is that what I’ve made the for CustomValidation is a bool that compares the selectedValue’s to make sure they are not the same. However, I can’t directly go to say ‘ddGroup01’ or ‘ddGroup02’ as it doesnt exists in the current context.
So my question, what do I need to do to get the DropDown Lists to exist for the server validation?
aspx page.
<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" />
A snippet of the code behind. Assuming this is the parts one would need.
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;
}
Thanks guys and gals. Look forward to the responses (love learning this stuff)
Cheers,
Trent
Since you will not have access to the edititem template in the scope of your validation method ,you have to do some bit of digging to locate the individual dropdowns.The EditIndex property of your grid view fortunately helps you to locate the controls painlessly.You should then do something like this
Markup
Code Behind
Let me know if this works.