Ans:-
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Check Box List with TextBox Operation</title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div style="border:solid:1px:gray">
<br />
Emp Names:<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
<br />
Selected Name: <asp:TextBox ID="txtId" runat="server"></asp:TextBox><br />
</div>
</center>
</form>
</body>
</html>
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Check Box List with TextBox Operation</title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div style="border:solid:1px:gray">
<br />
Emp Names:<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
<br />
Selected Name: <asp:TextBox ID="txtId" runat="server"></asp:TextBox><br />
</div>
</center>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection cn;
static SqlCommand cmd;
static SqlDataAdapter da;
static string strConn = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindChkData();
}
}
void BindChkData()
{
cn = new SqlConnection(strConn);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spGetEmpName";
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Emp");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "name";
CheckBoxList1.DataValueField = "id";
CheckBoxList1.DataBind();
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
string items1=string.Empty;
//int[] arr=new int[11];
//int i = 0;
ArrayList arr = new ArrayList();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true)
{
items1 += li.Text + ",";
txtId.Text = li.Value;
arr.Add(txtId.Text);
//arr[i] =Convert.ToInt32(txtId.Text);
//i++;
}
}
string[] item1 = items1.Split(',');
foreach (string s in item1)
{
Response.Write(s + " ");
}
//foreach (int j in arr)
//{
// Response.Write(j + " ");
//}
Response.Write("<br/>");
foreach (var s in arr)
{
Response.Write(s + " ");
}
}
}
web.config:-
<connectionStrings>
<add name="conStr" connectionString="server=192.168.0.200;database=subsdb;user id=sa;pwd=amtpl@123"/>
</connectionStrings>
Stored Procedure:-
create proc spGetEmpName
as
begin
select id,name from Emp;
end