[英]How to make required field validator on dropdownlist?
我有一個下拉列表,在其中我為(選擇頻道...)添加了額外的值。 如果選擇此值,則我希望顯示一條必需的消息,我嘗試的是:
<td>
<asp:DropDownList ID="ServiceChannelDropDownList" AppendDataBoundItems="true"
runat="server" DataTextField="Description" DataValueField="ID">
<asp:ListItem Value="-1" Text="Choose channel..." />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="-1"
ErrorMessage="*"
</asp:RequiredFieldValidator>
</td>
不幸的是,這只是讓我選擇了初始值(數據庫中自然不存在該初始值)而沒有說不允許這樣做。 如何解決呢?
您需要在綁定Dropdownlist數據的(.cs)后面的代碼中添加默認值,而不是在.aspx頁中添加默認值
ServiceChannelDropDownList.DataBind();
ServiceChannelDropDownList.Items.Insert(0, new ListItem("Choose channel...","-1"));
<td>
<asp:DropDownList ID="ServiceChannelDropDownList" AppendDataBoundItems="true"
runat="server" DataTextField="Description" DataValueField="ID">
<asp:ListItem Value="0" Text="Choose channel..." />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="0"
ErrorMessage="*"
</asp:RequiredFieldValidator>
</td>
我需要將ValidationGroup添加到RequiredFieldValidator,例如,
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="0"
ErrorMessage="Channel is required."
ValidationGroup="WorkflowValidation"
CssClass="ValidationCss"
EnableTheming="false">*
</asp:RequiredFieldValidator>
謝謝大家的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.