Asp.net 中使用必填字段验证器进行下拉列表验证

发布于 2024-10-22 04:17:51 字数 1098 浏览 6 评论 0原文

我有 Dropdownlist,其值字段和文本字段在运行时绑定。 它有 --select-- 作为第一项,值为 0 其余的值在运行时绑定。

我已将控件和验证器的验证组指定为 "g1"Intialvalue=0

但即使我选择 --select-- 选项,页面仍然会回发。

<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1" 
    ID="ddlReportType" runat="server" 
    OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>

<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server"  Display="Dynamic">
</asp:RequiredFieldValidator>

以及绑定下拉菜单的代码

ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;

I have Dropdownlist whose value field and text field are bind at runtime.
it has --select-- as first item with a value of 0
and the rest of the values are bind at runtime.

I have given validaton group for both the control and the validator as "g1"
and Intialvalue=0

But still the page is posting back even if I select --select-- option.

<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1" 
    ID="ddlReportType" runat="server" 
    OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>

<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server"  Display="Dynamic">
</asp:RequiredFieldValidator>

And code Behind to Bind the Dropdown

ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必在意 2024-10-29 04:17:51
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" 
    ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
    Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" 
    ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
    Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>
孤蝉 2024-10-29 04:17:51

这里使用 asp:CompareValidator,并将值与“select”选项进行比较。

使用 Operator="NotEqual" ValueToCompare="0" 阻止用户提交“选择”。

<asp:CompareValidator ControlToValidate="ddlReportType" ID="CompareValidator1"
    ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
    runat="server" Display="Dynamic" 
    Operator="NotEqual" ValueToCompare="0" Type="Integer" />

当您执行上述操作时,如果您从下拉列表中选择“select”选项,它将显示ErrorMessage

Here use asp:CompareValidator, and compare the value to "select" option.

Use Operator="NotEqual" ValueToCompare="0" to prevent the user from submitting the "select".

<asp:CompareValidator ControlToValidate="ddlReportType" ID="CompareValidator1"
    ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
    runat="server" Display="Dynamic" 
    Operator="NotEqual" ValueToCompare="0" Type="Integer" />

When you do above, if you select the "select " option from dropdown it will show the ErrorMessage.

清醇 2024-10-29 04:17:51

我为此苦苦挣扎了几天,直到我不得不构建一个新的下拉菜单时偶然发现了这个问题。我有几个 DropDownList 控件并尝试进行验证,但没有成功。一个是数据绑定的,另一个是从 aspx 页面填充的。我需要删除数据绑定列表并添加第二个手动列表。在我的例子中,如果您构建了这样的下拉列表并查看所需验证器或比较验证器的任何值(0 或 -1),验证器就会失败:

<asp:DropDownList ID="DDL_Reason" CssClass="inputDropDown" runat="server">
<asp:ListItem>--Select--</asp:ListItem>                                                                                                
<asp:ListItem>Expired</asp:ListItem>                                                                                                
<asp:ListItem>Lost/Stolen</asp:ListItem>                                                                                                
<asp:ListItem>Location Change</asp:ListItem>                                                                                            
</asp:DropDownList>

但是,像这样添加初始值对于比较验证器来说会立即起作用。

<asp:ListItem Text="-- Select --" Value="-1"></asp:ListItem>

I was struggling with this for a few days until I chanced on the issue when I had to build a new Dropdown. I had several DropDownList controls and attempted to get validation working with no luck. One was databound and the other was filled from the aspx page. I needed to drop the databound one and add a second manual list. In my case Validators failed if you built a dropdown like this and looked at any value (0 or -1) for either a required or compare validator:

<asp:DropDownList ID="DDL_Reason" CssClass="inputDropDown" runat="server">
<asp:ListItem>--Select--</asp:ListItem>                                                                                                
<asp:ListItem>Expired</asp:ListItem>                                                                                                
<asp:ListItem>Lost/Stolen</asp:ListItem>                                                                                                
<asp:ListItem>Location Change</asp:ListItem>                                                                                            
</asp:DropDownList>

However adding the InitialValue like this worked instantly for a compare Validator.

<asp:ListItem Text="-- Select --" Value="-1"></asp:ListItem>
糖果控 2024-10-29 04:17:51

在必填字段验证器标记中添加 InitialValue="0"

 <asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID"
      Display="Dynamic" ValidationGroup="g1" runat="server"
      ControlToValidate="ControlID"
      InitialValue="0" ErrorMessage="ErrorMessage">
 </asp:RequiredFieldValidator>

Add InitialValue="0" in Required field validator tag

 <asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID"
      Display="Dynamic" ValidationGroup="g1" runat="server"
      ControlToValidate="ControlID"
      InitialValue="0" ErrorMessage="ErrorMessage">
 </asp:RequiredFieldValidator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文