In ASP.NET, the GridView control offers powerful customization options through templates like ItemTemplate, EditItemTemplate, and HeaderTemplate. These templates allow developers to tailor the appearance and behavior of GridView rows and columns to meet specific requirements.
Steps to Use Templates in GridView:
-
Markup Setup:
- Define the GridView control in your ASPX markup.
- Configure necessary event handlers for editing, updating, canceling edit, and deleting rows.
-
Template Fields:
- Utilize TemplateFields to customize individual columns within the GridView.
- Specify HeaderTemplate for column headers to provide descriptive titles.
-
Data Binding:
- Implement data binding logic in the code-behind file to populate the GridView with data.
- Ensure that DataKeyNames property of the GridView is set to the primary key field.
-
Event Handling:
- Handle GridView events such as RowEditing, RowUpdating, RowCancelingEdit, and RowDeleting to manage user interactions with the GridView.
-
Update Logic:
- In the RowUpdating event handler, retrieve data from the edited row, perform necessary updates to the data source, and rebind the GridView.
Example Code:
<asp:GridView ID="GridView1" runat="server" CssClass="gridview" AutoGenerateColumns="False"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="ID">
<HeaderTemplate>
Employee ID
</HeaderTemplate>
<ItemTemplate>
<%# Eval("ID") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<HeaderTemplate>
Employee Name
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<HeaderTemplate>
Employee Age
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Age") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
// Simulated data source
List employees = new List()
{
new Employee() { ID = 1, Name = "John Doe", Age = 30 },
new Employee() { ID = 2, Name = "Jane Smith", Age = 25 },
new Employee() { ID = 3, Name = "Bob Johnson", Age = 40 }
};
GridView1.DataSource = employees;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox txtName = (TextBox)row.FindControl("txtName");
string name = txtName.Text;
// Update the data source (this is just a simulation)
// You would typically update your actual data source here
// For the sake of simplicity, I'll just display the updated values in a message box
Response.Write($"ID: {id}, New Name: {name}");
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
// Delete the data source entry (this is just a simulation)
// You would typically delete the record from your actual data source here
BindGrid();
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Implementing templates in GridView provides flexibility in designing interactive data displays. By leveraging ItemTemplate, EditItemTemplate, and HeaderTemplate, developers can create intuitive interfaces for viewing and editing tabular data in ASP.NET applications.
For a detailed example of a responsive GridView in ASP.NET, check out Responsive GridView Example in ASP.Net.