慎用GridView控件中的EmptyDataText属性(原创)

     "慎用"这里是小心使用的意思。GridView控件在项目中是比较常用的数据展现控件,那么你是否用过GridView的EmptyDataText属性呢?

MSDN上这样描述的:

GridView.EmptyDataText 属性:获取或设置在 GridView 控件绑定到不包含任何记录的数据源时所呈现的空数据行中显示的文本。

简言之:就是当绑定的数据源无数据的时候,即调用控件的DataBind()方法后页面上展示的信息。

 那么你是否注意到当数据源没有任何记录的情况下,生成的HTML源文件是怎样的呢?下面看看测试:

<asp:GridView ID="gvProcInstHist" AutoGenerateColumns="False" runat="server"  EmptyDataText="这里显示为空的数据"
                GridLines
="None" Width="100%">
                
<Columns>
                    
<asp:BoundField DataField="ProcessApproval" HeaderText="处理人"></asp:BoundField>
                    
<asp:BoundField DataField="UserOpinion" HeaderText="个人意见"></asp:BoundField>
                    
<asp:BoundField DataField="ProcessDate" HeaderText="处理时间"></asp:BoundField>
                
</Columns>
            
</asp:GridView>

 

 this.gvProcInstHist.DataSource = GetData();
 
this.gvProcInstHist.DataBind();

当调用上面语句且GetData方法获取的记录条数为0的时候,生成的HTML页面代码如下:

<table cellspacing="0" border="0" id="gvProcInstHist" style="100%;border-collapse:collapse;">
        
<tr>
            
<td colspan="3">这里显示为空的数据</td>
        
</tr>
    
</table>

可以看到<td colspan="3"></td>就说明三列合并成一列了。

可以想象如果有这样的要求,当数据源的条数为0的时候,也要把标题显示出来的话,那岂不是要在EmptyDataText属性中这样写:

EmptyDataText="<table><tr><th scope='col'>处理人</th><th scope='col'>个人意见</th><th scope='col'>处理时间</th></tr></table>"

是不是这样感觉到HTML很乱,而且这样样式在<td colspan="3"></td>中不是很好控制。

所以EmptyDataText属性就生成出来的HTML源代码是这样的:

<table cellspacing="0" border="0" id="gvProcInstHist" style="100%;border-collapse:collapse;">
        
<tr>
            
<td colspan="3"><table><tr><th scope='col'>处理人</th><th scope='col'>个人意见</th><th scope='col'>处理时间</th></tr></table></td>
        
</tr>
    
</table>

然而下面是另外一种解决方案:实现的是当数据源的条数为0的时候把标题也展示出来:(不使用GridView的EmptyDataText属性)

 <table width="100%" border="0" runat="server" id="tbProcInstHist" visible="false"
                cellspacing
="0">
                
<tr>
                    
<th width="15%">
                        处理人
                    
</th>
                    
<th>
                        个人意见
                    
</th>
                    
<th>
                        处理时间
                    
</th>
                
</tr>
                
<tr>
                    
<td>
                        
&nbsp;
                    
</td>
                    
<td>
                        
&nbsp;
                    
</td>
                    
<td>
                        
&nbsp;
                    
</td>
                
</tr>
            
</table>
            
<asp:GridView ID="gvProcInstHist" AutoGenerateColumns="False" runat="server" 
                GridLines
="None" Width="100%">
                
<Columns>
                    
<asp:BoundField DataField="ProcessApproval" HeaderText="处理人"></asp:BoundField>
                    
<asp:BoundField DataField="UserOpinion" HeaderText="个人意见"></asp:BoundField>
                    
<asp:BoundField DataField="processDate" HeaderText="处理时间"></asp:BoundField>
                
</Columns>
            
</asp:GridView>
增加一个表格在服务器端来控制是否显示表格还是GridView中的数据。
 this.gvProcInstHist.DataSource = GetData();
                
this.gvProcInstHist.DataBind();
                
if (gvProcInstHist.Rows.Count <= 0)
                {
                    
this.tbProcInstHist.Visible = true;
                    
this.gvProcInstHist.Visible = false;
                }
这样生成出来的HTML效果是:
  <table id="tbProcInstHist" width="100%" border="0" cellspacing="0">
    
<tr>
        
<th width="15%">
                        处理人
                    
</th>
        
<th>
                        个人意见
                    
</th>
        
<th>
                        处理时间
                    
</th>
    
</tr>
    
<tr>
        
<td>
                        
&nbsp;
                    
</td>
        
<td>
                        
&nbsp;
                    
</td>
        
<td>
                        
&nbsp;
                    
</td>
    
</tr>
</table>

是不是感觉到比前面那用EmptyDateText属性要简洁得多,代码也更规范,可读性样式都比较好控制。

当然我写这篇文章不是说不用EmptyDataText属性,这得根据实际情况。

个人观点:如果需要展示的只是简单的文本的话可以用EmptyDataText属性来设置,如果是比较复杂的标签的话建议还是不用EmptyDataText属性,可以用上面的方法来模拟一个标签来实现。

以上观点只是个人观点,仅供朋友们参考。如果朋友们有什么建议也请及时的指出。thx.

CharlesChen

Email:gotosunny@msn.com

原文地址:https://www.cnblogs.com/Charles2008/p/1355592.html