Dear All,
I have a scenario where I am using checkboxes inside a DataList.
I have a requirement that atleast one checkbox should be always selected.
I am trying to achieve the same using javascript but it is not working.
Can any body help me?
Below is the sample code:
aspx file :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="ESS.Components.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript">
function openWin2(u, k) {
window.open("../Pages/ELC_OrgLevelWiseSignatoryConfigurationPopUp.aspx" + u + " &Mode=" + k, "composerwindow", "toolbar=no,width=850,height=550,status=no,scrollbars=yes,resize=no,menubar=no");
}
function CheckBoxClicked(chk) {
debugger;
var dl = document.getElementById("<%=DataList1.ClientID%>");
var inputs = dl.getElementsByTagName("input");
var checked;
checked = false;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox")
{
if (inputs[i].id == chk.id)
{
if (inputs[i].checked == true);
{
checked = true;
break;
}
}
}
}
if (checked == false) {
alert("aaaa");
return false;
}
else {
return false;
}
}
</script>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:checkbox ID="Checkbox1" runat="server"
Text='<%#Eval("orglvl") %>' AutoPostBack="true" Checked="true"
oncheckedchanged="Checkbox1_CheckedChanged" onclick = "CheckBoxClicked(this)"></asp:checkbox>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
cs file :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace ESS.Components
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
filldatalist();
}
private void filldatalist()
{
DataTable dt = new DataTable();
dt.Columns.Add("orglvl", typeof(int));
DataRow row;
for(int i=1; i<=10; i++)
{
row = dt.NewRow();
row["orglvl"] = i;
dt.Rows.Add(row);
}
DataList1.DataSource = dt;
DataList1.DataBind();
}
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
Regards,
Gopal