【问题标题】:Validate textboxes and dropdowns inside GridView验证 GridView 中的文本框和下拉菜单
【发布时间】:2016-09-01 13:58:14
【问题描述】:

我已尝试在 StackOverflow 和其他网站上查找此内容,但找不到任何与我的问题相符的内容。基本上,我有一个带有下拉菜单的gridview 和一个包含在行中的文本框。用户需要从下拉列表中选择一个选项,根据他们选择的选项,他们可能还需要在同一行的文本框中添加一些数据。作为其中的一部分,我想运行验证以提醒用户他们尚未完成哪些字段 - 但如果可能的话,我想尝试在 asp.net C# 中执行此操作(不使用 JavaScript 或 JQuery 或其他任何东西,如果我可以避免)。

到目前为止,我已经能够做到:如果未选择下拉菜单或未输入文本,则 gridview 将验证 - 但它会针对每一行显示错误消息,即使只有一行不正确,而我只希望验证消息出现在那些没有出现的行上。

我的代码是:

<asp:UpdatePanel ID="upCurrentServicesGrid" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:GridView ID="grdviewCurrentSystems" CssClass="table-data currentSystemTable" UseAccessibleHeader="true" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:BoundField DataField="systemName" HeaderText="Service" ItemStyle-Width="10%"/>

                <asp:TemplateField HeaderText="Status" ItemStyle-Width="39%">
                    <ItemTemplate>
                        <asp:DropDownList ID="aspdropSystemStatus" CausesValidation="false" runat="server" />
                            <div style="display:none">
                            <asp:TextBox ID="RowId" runat="server" Text='<%# Eval("id") %>' />
                            </div>
                            <div style="display:none">
                                <asp:TextBox ID="SelectedVal" runat="server" Text='<%# Eval("systemStatus") %>' />
                            </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Further information" ItemStyle-Width="50%">
                    <ItemTemplate>
                        <div>
                            <asp:TextBox ID="txtSystemStatusInfo" TextMode="MultiLine" MaxLength="300" CssClass="textarea1" runat="server" Text='<%# Eval("systemInformation") %>' />
                                <asp:CustomValidator
                                    CssClass="CustomValidator1"
                                    OnServerValidate="grdviewFurtherInfo_Validate" 
                                    Display="Dynamic"
                                    runat="server"
                                    ValidationGroup="valgrpLocal">
                                        <asp:ValidationMessageLocal 
                                            CssClass="aspvalmsgLocal" 
                                            ID="aspvalSystemStatus" 
                                            Title="Service statuses" 
                                            TextPassed="" 
                                            TextFailed="Check Status or Further Information fields" 
                                            runat="server" />
                                    </asp:CustomValidator>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                    <ItemTemplate>
                        <div>
                            <asp:LinkButton ID="lnkbtnDelSystem" CausesValidation="false" OnClick="lnkbtnDelSystem_Click" OnClientClick="return confirmDelete(this.id); return false;" CommandArgument='<%# Eval("id") %>' runat="server">
                                <span class="btn btn-small btn-spinner btn-grid">X</span> 
                            </asp:LinkButton>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

而我背后的代码是:

protected void grdviewFurtherInfo_Validate(object sender, ServerValidateEventArgs e)
{

    bool valid = true;
    foreach (GridViewRow row in grdviewCurrentSystems.Rows)
    {
        usercontrols_asp_formctl_dropdownlist aspDrop = (usercontrols_asp_formctl_dropdownlist)row.FindControl("aspdropSystemStatus");
        usercontrols_asp_formctl_textbox aspTxt = (usercontrols_asp_formctl_textbox)row.FindControl("txtSystemStatusInfo");


        if (aspDrop.SelectedValue == "")
        {
            valid = false;
        }
        else if (aspDrop.SelectedValue != "1" && string.IsNullOrEmpty(aspTxt.Text))
        {
            valid = false;
        }
        e.IsValid = valid;
    }


}

谁能给点建议?

谢谢! :)

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

试试这个,而不是每个循环。

    int selRowIndex = ((GridViewRow)(((DropDownList )sender).Parent.Parent)).RowIndex;
         usercontrols_asp_formctl_dropdownlist aspDrop = (usercontrols_asp_formctl_dropdownlist)grdviewCurrentSystems.Rows[selRowIndex].FindControl("aspdropSystemStatus");
            usercontrols_asp_formctl_textbox aspTxt = (usercontrols_asp_formctl_textbox)grdviewCurrentSystems.Rows[selRowIndex].FindControl("txtSystemStatusInfo");
 if (aspDrop.SelectedValue == "")
        {
            valid = false;
        }
        else if (aspDrop.SelectedValue != "1" && string.IsNullOrEmpty(aspTxt.Text))
        {
            valid = false;
        }
        e.IsValid = valid;

【讨论】:

  • 不幸的是,这似乎不起作用,我收到一个错误:无法将“System.Web.UI.WebControls.CustomValidator”类型的对象转换为“System.Web.UI.WebControls” 。下拉列表'。我对整个 asp.net 还是很陌生,所以我不确定我在这里做了什么。
【解决方案2】:

您可以为 GridView 中的每一行使用CustomValidator

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Input incorrect" ValidationGroup='<%# "valGroup_" + Container.DataItemIndex %>' ClientValidationFunction="myValidator" ControlToValidate="aspdropSystemStatus" Display="Dynamic"></asp:CustomValidator>

对于gridview之外的客户端脚本:

<script type="text/javascript">
    function myValidator(oSrc, args) {
        //get the rownumber that fires the validator
        var rowNumber = oSrc.id.split("_")[3];
        //get the id of the dropdown of the row
        var DropDownID = "mainContentPane_grdviewCurrentSystems_aspdropSystemStatus_" + rowNumber;
        //get the id of the textbox of the row
        var TextBoxID = "mainContentPane_grdviewCurrentSystems_txtSystemStatusInfo_" + rowNumber;
        //is the row correctly filled out
        if (document.getElementById(DropDownID).selectedIndex > 0 && document.getElementById(TextBoxID).value == "") {
            args.IsValid = false;
        } else {
            args.IsValid = true;
        }
    }
</script>

为此,您必须执行以下操作:

ValidationGroup='&lt;%# "valGroup_" + Container.DataItemIndex %&gt;' 添加到行和保存按钮中需要验证的每个元素。通过将Container.DataItemIndex(行号)添加到名称中,验证组名称是唯一的。如果它不是唯一的,它将验证所有行作为一个组,这不是你想要的。

检查oSrc.id.split("_")[3]; 是否为您获取行号。在我的情况下,ID 看起来像这样mainContentPane_grdviewCurrentSystems_CustomValidator1_1,这就是为什么我们需要在拆分 ID 后的第三个值。

检查您要验证的 DropDown 和 TextBox(es) 的 id,并将它们设置为 javascript 中的变量。如果 ID 看起来像这样 mainContentPane_grdviewCurrentSystems_aspdropSystemStatus_0,只需删除行号并将其替换为 javascript 的 rowNumber

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多