Skip to main content

Binding Data to a Web Forms DataList




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListTest.aspx.cs"
Inherits="DataListTest" %>


<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dataList" Style="z-index: 102; left: 16px; position: absolute;
top: 56px"
runat="server" OnCancelCommand="dataList_CancelCommand" OnDeleteCommand="dataList_DeleteCommand"
OnEditCommand="dataList_EditCommand" OnItemCommand="dataList_ItemCommand" OnUpdateCommand="dataList_UpdateCommand">
<SelectedItemTemplate>
<asp:Button ID="editButton" runat="server" Text="Edit" CommandName="Edit"></asp:Button>
<b>
<%# DataBinder.Eval(Container.DataItem, "Id") %>
;
<%# DataBinder.Eval(Container.DataItem, "IntField") %>
;
<%# DataBinder.Eval(Container.DataItem, "StringField") %>
</b>
</SelectedItemTemplate>
<ItemTemplate>
<asp:Button ID="selectButton" runat="server" Text="Select" CommandName="Select"></asp:Button>
<%# DataBinder.Eval(Container.DataItem, "Id") %>
;
<%# DataBinder.Eval(Container.DataItem, "IntField") %>
;
<%# DataBinder.Eval(Container.DataItem, "StringField") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="updateButton" runat="server" Text="Update" CommandName="Update"></asp:Button>
<asp:Button ID="deleteButton" runat="server" Text="Delete" CommandName="Delete"></asp:Button>
<asp:Button ID="cancelButton" runat="server" Text="Cancel" CommandName="Cancel"></asp:Button>
<br>
<asp:Label ID="Label1" runat="server">ID: </asp:Label>
<asp:TextBox ID="idTextBox" runat="server" Width="96px" ReadOnly="True" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>'>
</asp:TextBox>
<br>
<asp:Label ID="Label2" runat="server">IntField: </asp:Label>
<asp:TextBox ID="intFieldTextBox" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"IntField") %>'
>
</asp:TextBox>
<br>
<asp:Label ID="Label3" runat="server">StringField: </asp:Label>
<asp:TextBox ID="stringFieldTextBox" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"StringField") %>'
>
</asp:TextBox>
</EditItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class DataListTest : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindList();

}

}

private void BindList()
{
if (Session["DataSource"] == null)
{
dataList.DataSource = GetDataSet();
dataList.DataKeyField = "Id";
}
else
{
dataList.DataSource = (DataTable)Session["DataSource"];
dataList.DataKeyField = "Id";
}
dataList.DataBind();
}
public DataTable GetDataSet()
{

DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("IntField", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringField", typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i;
dr[2] = "Company" + i + Environment.NewLine + "Title" + i;
dt.Rows.Add(dr);
DataColumn[] Parent_PKColumns = new DataColumn[1];
Parent_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Parent_PKColumns;
Session["DataSource"] = dt;
}

return dt;
}

protected void dataList_CancelCommand(object source, DataListCommandEventArgs e)
{
// Set the index of the item being edited out of range.
dataList.EditItemIndex = -1;

BindList();

}
protected void dataList_EditCommand(object source, DataListCommandEventArgs e)
{
// Set the index of the selected item out of range.
dataList.SelectedIndex = -1;
// Set the index of the item being edited to the current record.
dataList.EditItemIndex = e.Item.ItemIndex;
BindList();
}
protected void dataList_DeleteCommand(object source, DataListCommandEventArgs e)
{
// Get the data from the session variable.
DataTable dt = (DataTable)Session["DataSource"];

// Get the ID of the row to delete.
int id = (int)dataList.DataKeys[e.Item.ItemIndex];

// Delete the row from the table.
dt.Rows.Find(id).Delete();

// Update the data source with the changes to the table.
UpdateDataSource(dt);

// Set the index of the item being edited out of range.
dataList.EditItemIndex = -1;




BindList();

}

public void UpdateDataSource(DataTable dt)
{

dt.AcceptChanges();
Session["ds"] = dt;
}
protected void dataList_ItemCommand(object source, DataListCommandEventArgs e)
{
// Check if the "select" button is pressed.
if (e.CommandName == "Select")
{
// Set the index of the item being edited out of range.
dataList.EditItemIndex = -1;
// Set the index of the selected item to the current record.
dataList.SelectedIndex = e.Item.ItemIndex;

BindList();
}
}
protected void dataList_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Get the data from the session variable.
DataTable dt = (DataTable)Session["DataSource"];

// Get the ID of the row to update.
int id = (int)dataList.DataKeys[e.Item.ItemIndex];

// Get the DataRow to update using the ID.
DataRow dr = dt.Rows.Find(id);

// Get the column values for the current record from the DataList.
dr["IntField"] =
Int32.Parse(((TextBox)e.Item.FindControl("intFieldTextBox")).Text);
dr["StringField"] =
((TextBox)e.Item.FindControl("stringFieldTextBox")).Text;

// Update the data source with the changes to the table.
UpdateDataSource(dt);

// Set the index of the item being edited out of range.
dataList.EditItemIndex = -1;

BindList();

}
}

Comments

Popular posts from this blog