CheckBox tool in DataList in Asp.net
This Article explain the how to use Checkbox tool in DataList Control and its use in Asp.net program
#Make new project in Asp.net and add webform then add datalist
#Connection String
<connectionStrings>
<add name="ps" connectionString="server=.\SQLEXPRESS;Database=your db name ;Integrated Security=True"/>
</connectionStrings>
|
#Coding
protected void Page_Load(object sender,
EventArgs e)
{
if(Page.IsPostBack==false)
{
List_Bind();
}
}
private void List_Bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["ps"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
|
# Html Coding
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<b>Title : </b><asp:LinkButton ID="lk" Text='<%#Eval("BookTitle")%>' runat="server" CommandName="select" /><br />
<b>Author : </b><%#Eval("BookAuthor")%><br />
<b>Publisher : </b><%#Eval("BookPublisher")%><br />
<b>Price : </b><%#Eval("BookPrice")%><br />
<asp:CheckBox ID="chk" runat="server" /><b><i>Buy</i></b>
</ItemTemplate>
</asp:DataList>
|
*Repeat Column =2
Add Button and Repeater
#Button Coding
protected void Button1_Click(object sender,
EventArgs e)
{
string d = "";
for (Int32 i=0;i<DataList1.Items.Count;i++)
{
CheckBox ch;
ch =
(CheckBox)(DataList1.Items[i].FindControl("chk"));
if (ch.Checked)
{
d +=
DataList1.DataKeys[i].ToString() + ",";
}
}
d = d.Substring(0, d.Length - 1);
// Label1.Text = d;
string qry = "select * from tbbook where BookId in(" + d + ")";
SqlDataAdapter adp = new SqlDataAdapter(qry,
ConfigurationManager.ConnectionStrings["ps"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
|
d = d.Substring(0, d.Length - 1); - To remove Extra Comma
From empno
No comments:
Post a Comment