Saturday, December 21, 2013

Asp.Net databound field shows   character when null or empty.

When working with gridview in Asp.Net one of the most common behaviour that is often seen is   in bound Field Columns. This is the case when datasouce is having empty string which is binded to the gridview.

Whenever in the datasource which is being binded to the gridview has empty string (""), it is mostly retrieved as  .

To address this, or if you need some default value instead of  , the BoundField class provided "NullDisplayText" property which can be used.

"NullDisplayText"  Gets or sets the caption displayed for a field when the field's value is null. [as per msdn],

Even if the field value is empty string ("") and if you set "NullDisplayText" as " ", this would no more give you   as empty string ("") is rendered as  .

Therefore you do not need to put any additional checks in the code to handle  .

[GridView code .aspx page]
 <asp:GridView ID="grdTestGrid" runat="server" AutoGenerateColumns="false" onrowcommand="grdTestGrid_RowCommand">  
       <asp:BoundField DataField="name" HeaderText="Name" NullDisplayText=" "/>  
       <asp:BoundField DataField="email" HeaderText="Email"/>  
       <asp:TemplateField HeaderText="GetValue">  
           <asp:Button ID="btnGetValue" runat="server" Text="Get Value" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' UseSubmitBehavior="False" />  


The above code for gridview has two BoundField's name and email. In one of the field which is "name" we are using NullDisplayText property and setting its value to " ". Due to this if the binded value from source comes as null or an empty string ("") it would give you " " value instead of &nbsp; and hence would not require any special handling on code-behind for &nbsp;.

The second field value on retrieval would display &nbsp; if the binding value from source comes as null or an empty string ("").

[GridView RowCommand code .aspx.cs file]
1:  using System;  
2:  using System.Collections.Generic;  
3:  using System.Linq;  
4:  using System.Web;  
5:  using System.Web.UI;  
6:  using System.Web.UI.WebControls;  
7:  using System.Data;  
8:  public partial class _Default : System.Web.UI.Page  
9:  {  
10:    DataTable dataTable;  
11:    DataRow dataRow;  
12:    protected void Page_Load(object sender, EventArgs e)  
13:    {  
14:      dataTable = new DataTable();  
15:      dataTable.Columns.Add("name", typeof(string));  
16:      dataTable.Columns.Add("email", typeof(string));  

17:      dataRow = dataTable.NewRow();  
18:      dataRow["name"] = "Vivek Joshi";  
19:      dataRow["email"] = "";  
20:      dataTable.Rows.Add(dataRow);  

21:      dataRow = dataTable.NewRow();  
22:      dataRow["name"] = "";  
23:      dataRow["email"] = "";  
24:      dataTable.Rows.Add(dataRow);  

25:      grdTestGrid.DataSource = dataTable;  
26:      grdTestGrid.DataBind();  
27:    }  

28:    protected void grdTestGrid_RowCommand(object sender, GridViewCommandEventArgs e)  
29:    {  
30:      int rowIndex = int.Parse(e.CommandArgument.ToString());  
31:      string strName = grdTestGrid.Rows[rowIndex].Cells[0].Text;  
32:      string strEmail = grdTestGrid.Rows[rowIndex].Cells[1].Text;  
33:    }  
34:  }  


Page Load:
In the code-behind code above, we have created a datasource which is having two rows of which one row has values and the other does not have anything. The second row for now has been assigned as empty string (""). null can also be used as well for testing.

Grid RowCommand:
In this event we are retrieving the value of cells for which the event is raised. For first row all would be well as we neither have null nor an empty string ("") for both cells.

For the second row both the cells have empty string (""). As we are using The NullDisplayText=" " for BoundField "name" we would get " " on retrieving the value, but for the BoundField email we would get &nbsp;.

This is how we can avoid &nbsp;. Also this is just an other way for handling &nbsp;.

If you have any querries or sugesstions, please leave a comment.

Vivek Joshi

No comments: