I have one datalist control which has some records, It also has child control checkbox named cb1 in the itemtemplate of datalist.
All I want to do is on the click of a submit button on page, all the checkboxed records should show up in the repeater down below. I managed to get the item.index value of the checked records into a array. But I don't know how to show the selected data into the repeater.
Here is an image of the page. Image
My code
private void list_bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string a = null;
foreach (DataListItem item in DataList1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("cb1");
if(chk.Checked)
{
a = Convert.ToString(DataList1.DataKeys[item.ItemIndex]);
}
}
}
}
Html code
<body>
<form id="form1" runat="server">
<asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" Height="206px" RepeatColumns="3" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" DataKeyField="bookid">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<ItemTemplate>
<b>Title: </b><%#Eval("booktit")%><br />
<b>Author: </b><%#Eval("bookaut") %><br />
<b>Pub: </b><%#Eval("bookpub") %><br />
<b>Price: </b><%#Eval("bookprc") %><br />
<asp:CheckBox ID="cb1" Text="Buy" runat="server" />
</ItemTemplate>
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DataList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<b>Title: </b><%#Eval("booktit")%><br />
<b>Author: </b><%#Eval("bookaut") %><br />
<b>Pub: </b><%#Eval("bookpub") %><br />
<b>Price: </b><%#Eval("bookprc") %><br />
</ItemTemplate>
</asp:Repeater>
</form>
Aucun commentaire:
Enregistrer un commentaire