Datalist asp.net c# example
In SQL-Server
create table emp(empid int,empname varchar(50),empdesc varchar(500))
datalist.aspx.cs
<asp:DataList Width="43%" ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table width="50%">
<tr bgcolor="silver">
<td width="50%">
<asp:Label ID="lbl_1" runat="server" Text="first"></asp:Label>
</td>
<td width="50%">
<asp:Label ID="lbl_2" runat="server" Text="second"></asp:Label>
</td>
</tr>
<tr bgcolor="lime">
<td width="100%" colspan="2">
<asp:TextBox ID="txt_1" TextMode="multiLine" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
datalist.cs
SqlDataAdapter da;
DataTable dt= new DataTable();
int counter = 0;
in pageload event call this bind() method:
bind()
{
SqlConnection con = new SqlConnection("server=.;database=demo;integrated security=true;");
con.Open();
da = new SqlDataAdapter("select * from emp", con);
da.Fill(dt);
con.Close();
DataList1.DataSource = dt;
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Label lbl1 = (Label)e.Item.FindControl("lbl_1");
lbl1.Text = dt.Rows[counter][0].ToString();
Label lbl2 = (Label)e.Item.FindControl("lbl_2");
lbl2.Text = dt.Rows[counter][1].ToString();
TextBox txt = (TextBox)e.Item.FindControl("txt_1");
txt.Text = dt.Rows[counter][2].ToString();
counter++;
}
No comments:
Post a Comment