我是c#编程的新手
我有一个radiobuttonlist(它包含两个radiobuttons employee
和department
)和一个下拉列表。如果我在没有任何radiobuttonlist选择的情况下选择下拉列表,我想显示错误消息。如果有一个radiobutton,我们可以使用radiobutton.ckecked属性。如何在radiobutton列表中完成相同的操作?我的设计来源是
<asp:RadioButtonList ID="rbllist" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Width="297px"
onselectedindexchanged="rbllist_SelectedIndexChanged">
<asp:ListItem Text="Employee" Value="1" ></asp:ListItem>
<asp:ListItem Text="Department" Value="2"></asp:ListItem>
</asp:RadioButtonList>
<table class="ui-accordion">
<tr>
<td align="left" class="style2">
<asp:Label ID="lbldept" runat="server" Text="Department"></asp:Label></td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:DropDownList ID="dddept" runat="server" AutoPostBack="True"
onselectedindexchanged="dddept_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
是不是?
if (rbllist.SelectedIndex == -1)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Please select one radiobuttonlist filed ..!!');", true);
}
答案 0 :(得分:0)
您可以使用RequiredFieldValidator
<asp:DropDownList ID="dddept" runat="server" AutoPostBack="True"
onselectedindexchanged="dddept_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"
ValidationGroup="g1" runat="server" ControlToValidate="dddept"
Text="*" ErrorMessage="Please Select Dept"></asp:RequiredFieldValidator>
OR
<asp:DropDownList ID="dddept" runat="server" AutoPostBack="True"
onselectedindexchanged="dddept_SelectedIndexChanged">
<asp:ListItem Text="--Select--" Value="0"/>
<asp:ListItem Text="Value1" Value="1"/>
</asp:DropDownList>
<asp:RequiredFieldValidator InitialValue="0" ID="Req_ID" Display="Dynamic"
ValidationGroup="g1" runat="server" ControlToValidate="dddept"
Text="*" ErrorMessage="Please Select Dept"></asp:RequiredFieldValidator>
答案 1 :(得分:0)
<asp:RadioButtonList ID="rbllist" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Width="297px"
onselectedindexchanged="rbllist_SelectedIndexChanged">
<asp:ListItem Text="Employee" Value="1" ></asp:ListItem>
<asp:ListItem Text="Department" Value="2"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="rbllist"
ErrorMessage="Select atleast anyone"
>
</asp:RequiredFieldValidator>
试试这个......
答案 2 :(得分:0)
如果您想首先在客户端验证,您可以执行以下操作,因为您使用的是ASP.NET 4
注意:最好始终在服务器端进行验证,可能是在提交或相关事件之后。
设置&#34; ClientIDMode&#34;到&#34;静态&#34;在radiobuttonlist和下拉列表中,如下所示:
<asp:RadioButtonList ID="rbllist" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" Width="297px"
OnSelectedIndexChanged="rbllist_SelectedIndexChanged" ClientIDMode="Static">
<asp:DropDownList ID="dddept" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="dddept_SelectedIndexChanged" ClientIDMode="Static">
您可以使用Javascript / Jquery进行客户端检查。但在Jquery中你可以检查如下:
<script>
$("select[id$=dddept]").click(function () {
var isOneRadioSelected = false;
$("#rbllist").find(":radio").each(function (index, radio) {
isOneRadioSelected = radio.checked;
if (isOneRadioSelected == true)
return false;
});
if (isOneRadioSelected == false) {
alert("Please select atleast one radio button."); //Here you can do proper error display format based on your application requirement.
}
});
</script>