[英]Gridview RadioButtonList Validation using javascript
I have one grid-view inside grid-view using one RadioButtonList
and outside grid-view using one asp.net Button
when user click that button all radio button list should be selected, then only that Button
event should be submitted. 当用户单击该按钮时,应选择所有单选按钮列表,然后仅应提交那个
Button
事件,而我使用一个RadioButtonList
可以在一个网格视图内使用一个网格视图,而使用一个asp.net Button
在一个外部网格视图中使用。
Below is my grid-view RadioButtonList
. 下面是我的网格视图
RadioButtonList
。
<asp:TemplateField HeaderText="Add Score">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="cssrbt">
<asp:ListItem Text="Correct" Value="1"></asp:ListItem>
<asp:ListItem Text="Wrong" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
Below is my button which using out side of gridview. 下面是我的按钮,它使用gridview的外面。
<asp:Button ID="btn_Submit" runat="server" class="btn btn-md btn-danger" Text="Submit" OnClick="btn_Submit_Click" ValidationGroup="viva" />
My issue is without selection inserting all grid-view data and that I want to validate. 我的问题是没有选择插入所有网格视图数据并且我想验证。
You can bind javascript client event to button and iterate each radiobuttonlist to validate if atleast on radio button is selected. 您可以将javascript客户端事件绑定到按钮,并迭代每个单选按钮列表以验证是否选择了“至少选择单选按钮”。
$('<%= btn_Submit.ClientID %>').click(function(){
$('.cssrbt').each(function(){
if($(this).find(':radio:selected').length == 0)
console.log("One of radio button list option must be selected")
});
});
The example that adil gave needs JQuery library to implement and if you dont want to use any library only javascript you can validate like this adil给出的示例需要JQuery库来实现,如果您不想只使用任何库,则可以像这样进行验证
<form runat="server" id="form1" action="Test.aspx.cs">
<asp:TemplateField HeaderText="Add Score">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="cssrbt">
<asp:ListItem Text="Correct" Value="1"></asp:ListItem>
<asp:ListItem Text="Wrong" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:Button ID="btn_Submit" runat="server" class="btn btn-md btn-danger" Text="Submit" OnClientClick="return Validate('RadioButtonList1')" OnClick="btn_Submit_OnClick" />
</form>
<script language="javascript" type="text/javascript">
function Validate(id) {
var radiobutton = document.getElementsByName(id);
var flag;
for (var j = 0; j < radiobutton.length; j++) {
if (radiobutton[j].checked !== true) {
flag = true;
} else {
break;
}
}
if (flag) {
alert("Please select one option");
return false;
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.