Introduction
DataList is a Databound control to display and manipulate data in a web application. It is a composite control that can combine other ASP.Net controls and it is present in the form. The DataList appearance is controlled by its template fields.
The following template fields are supported by the DataList control:
- Itemtemplate: It specifies the Items present in the Datasource, it renders itself in the browser as many rows present in the data source collection.
- EditItemTemplate: Used to provide edit permissions to the user.
- HeaderTemplate: Used to display header text to the data source collection.
- FooterTemplate: Used to display footer text to the data source collection.
- ItemStyle: Used to apply styles to an ItemTemplate.
- EditStyle: Used to apply styles to an EditItemTemplate
- HeaderStyle: Used to apply styles to a HeaderTemplate
- FooterStyle: Used to apply styles to a FooterTemplate.
Frequently used properties with DataList
Frequently used Events with DataList control
Event: Event is a precise time that something happened.
The following is an example demonstrate the DataList Control with a bound image and a FileUpload control:
Default.aspx
Default.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- using System.Web.Configuration;
-
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Bind();
- }
- }
- public void Bind()
- {
- SqlCommand cmd = new SqlCommand("select * from Employee", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds, "Employee");
- dl1.DataSource = ds.Tables[0];
- dl1.DataBind();
- }
- protected void dl1_EditCommand(object sender, DataListCommandEventArgs e)
- {
- dl1.EditItemIndex = e.Item.ItemIndex;
- Bind();
- }
- protected void dl1_CancelCommand(object sender, DataListCommandEventArgs e)
- {
- dl1.EditItemIndex = -1;
- Bind();
- }
- protected void dl1_updateCommand(object sender, DataListCommandEventArgs e)
- {
- int index = e.Item.ItemIndex;
- int empid = Convert.ToInt32(((TextBox)dl1.Items[index].FindControl("txtempid")).Text);
- string empname = ((TextBox)dl1.Items[index].FindControl("txtempname")).Text;
- string empmail = ((TextBox)dl1.Items[index].FindControl("txtempmail")).Text;
- long empmbnum = Convert.ToInt64(((TextBox)dl1.Items[index].FindControl("txtmbnum")).Text);
- FileUpload fu = (FileUpload)dl1.Items[index].FindControl("fu1");
-
- if (fu.HasFile)
- {
- string filepath = System.IO.Path.Combine(Server.MapPath("~/Images/"), fu.FileName);
- fu.SaveAs(filepath);
- SqlCommand fcmd = new SqlCommand();
- fcmd.CommandText = "update employee set EmpImage='"+fu.FileName+"'";
- fcmd.Connection = con;
- con.Open();
- fcmd.ExecuteNonQuery();
- con.Close();
- }
- SqlCommand cmd = new SqlCommand("update Employee set EmpName = '" + empname + "',EmpEmailId='" + empmail + "',EmpMobileNum =" + empmbnum + " where EmpId = " + empid + "", con);
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
- if (i == 1)
- {
- Response.Write("<script>alert('Successfully updated')</script>");
- dl1.EditItemIndex = -1;
- Bind();
- }
- }
- protected void dl1_DeleteCommand(object sender, DataListCommandEventArgs e)
- {
- int index = e.Item.ItemIndex;
- Label empid;
- empid = (Label)dl1.Items[index].FindControl("lblempid");
-
- SqlCommand cmd = new SqlCommand("delete from employee where EmpId = " + Convert.ToInt32(empid.Text) + "", con);
- con.Open();
- int res = cmd.ExecuteNonQuery();
- con.Close();
- if (res == 1)
- {
- Response.Write("<script>alert('Successfully deleted')</script>");
- Bind();
- }
-
- }
- }