Introduction
The empty data row is displayed in a GridView control when the data source that is bound to the control does not contain any records. You can define your own custom user interface (UI) for the empty data row by using the EmptyDataTemplate property. Below I'm giving a sample screen where it's displaying "No Records Found" message if the GridView control does not contain any records and at the same time we can capture new employee information also. Please click here to check the full article.
Background
This code is based on my another article in ASP.NET GridView Insert, Update and Delete, please click here to check the same. Here I'm explaining the code only related to EmptyDataTemplate.
Using the Code
HTML
Below, I have given the EmptyDataTemplete ASPX code for the Gridview, I have created a <table> and added the "No Records Found" message in a <tr> and in another <tr> I have given all controls that will be used to enter Employee information in-case of no existing records.
<EmptyDataTemplate>
<table class="grid" cellspacing="0" rules="all"
border="1" id="gvEG" style="border-collapse: collapse;">
<tr>
<th align="left" scope="col">
Employee Code
</th>
<th align="left" scope="col">
Name
</th>
<th align="left" scope="col">
Department
</th>
<th align="left" scope="col">
Group
</th>
<th align="left" scope="col">
Email
</th>
<th scope="col">
Ative
</th>
<th align="left" scope="col">
Edit
</th>
<th scope="col">
Delete
</th>
</tr>
<tr class="gridRow">
<td colspan="8">
No Records found...
</td>
</tr>
<tr class="gridFooterRow">
<td>
<asp:TextBox ID="txtEmployeeCode" runat="server"
MaxLength="6" Width="50px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmployeeName" runat="server"
Width="90px"></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server"
DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlEmployeeGroup" runat="server">
<asp:ListItem Text="User" Value="User"
Selected="True"></asp:ListItem>
<asp:ListItem Text="Admin" Value="Admin"></asp:ListItem>
<asp:ListItem Text="Super User"
Value="Super User"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" Width="100px" />
</td>
<td>
<asp:CheckBox ID="chkActive" runat="server" />
</td>
<td colspan="2" align="justify" valign="middle">
<asp:LinkButton ID="lnkAdd" runat="server"
CausesValidation="false" CommandName="emptyInsert"
Text="Insert"></asp:LinkButton>
</td>
</tr>
</table>
</EmptyDataTemplate>
Code-Behind
In RowCommand, I capture the Link Button click from the EmpltyDataTemlate. Here, we check the commandName and if it has the value"emptyInsert", then create the object of EmployeeInfo.
We can get the EmptyDateTemplate reference in code-behind by the below given code (gvEG is my GridView name).
GridViewRow emptyRow = gvEG.Controls[0].Controls[0] as GridViewRow;
Once we have the GridViewRow, then we can use the below code to find the controls:
emptyRow.FindControl("txtEmployeeCode")
protected void gvEG_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("emptyInsert"))
{
EmployeeInfo eInfo = new EmployeeInfo();
GridViewRow emptyRow = gvEG.Controls[0].Controls[0] as GridViewRow;
eInfo.EmployeeCode = Convert.ToString
(((TextBox)emptyRow.FindControl("txtEmployeeCode")).Text);
eInfo.EmployeeName = ((TextBox)emptyRow.FindControl("txtEmployeeName")).Text;
eInfo.DepartmentId = Convert.ToInt32(((DropDownList)
emptyRow.FindControl("ddlDepartment")).SelectedValue);
eInfo.DepartmentName = Convert.ToString(((DropDownList)
emptyRow.FindControl("ddlDepartment")).SelectedItem.Text);
eInfo.EmployeeGroup = ((DropDownList)emptyRow.FindControl
("ddlEmployeeGroup")).SelectedValue;
eInfo.Email = ((TextBox)emptyRow.FindControl("txtEmail")).Text;
eInfo.isActive = ((CheckBox)emptyRow.FindControl("chkActive")).Checked;
new mainSQL().insertEmployeeInfo(eInfo);
FillEmployeeGrid();
}
}
Summary
I hope this tip helped you to understand how to find controls from EmptyDataTemplate of ASP.NET GridView. Please check the full article from this link at ASP.NET GridView Insert, Update and Delete.
Please give your valuable suggestions and feedback for further improvements. Thanks for reading.