1

希望这对于那里的专家来说是一个相对快速的问题!

我有一个更新 GridView 函数,它有 5 个下拉列表。这 5 个列表绑定到同一个 DataTable,但最终用户(客户端)不能多次指定同一个 SelectedValue。Group01 被RequiredFieldValidator 覆盖,因此它们必须至少指定一个组。

我遇到的问题是,我为 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;
}

谢谢大家。期待回复(喜欢学习这些东西)

干杯,

特伦特

4

1 回答 1

1

由于您将无法访问验证方法范围内的 edititem 模板,因此您必须进行一些挖掘以找到各个下拉列表。幸运的是,网格视图的 EditIndex 属性可以帮助您轻松定位控件。您应该然后做这样的事情

标记

<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"  OnServerValidate="val_sameDropDown_ServerValidate" ControlToValidate="ddGroup01" Display="None" ></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateField>

代码背后

  protected void val_sameDropDown_ServerValidate(object source, ServerValidateEventArgs args)
{
DropDownlist ddGroup01 = (DropDownList)YourGridView.Rows(YourGridView.EditIndex).FindControl("ddGroup01");
//Now you can use the selected value as you wish.

}

让我知道这个是否奏效。

于 2013-01-21T16:06:25.260 回答