当前位置: 代码迷 >> ASP.NET >> 各位!
  详细解决方案

各位!

热度:2624   发布时间:2013-02-26 00:00:00.0
各位大虾救命啊!!!
datalist 绑定了字段 如果在后台查询的所有字段都为空 包括绑定的字段也为空  
我想在前台判断 如果字段为空 要给绑定的字段 赋其它指定的值 怎么处理

根据特定条件 查出来所有字段都没数据 用System.DBNull.Value判断行吗? 还是System.DBNull.Value 判断的是有记录 主键及其它一些字段 而绑定的字段为空 才有用

<asp:DataList ID="DataList1" runat="server" DataKeyField="adbookId"
  RepeatColumns="3" RepeatDirection="Horizontal">
  <ItemTemplate>
  <div id="content">
  <table border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td style="width: 360px; text-align: center;">
  <a href="#">
  <img src='<%#("imagepath")==System.DBNull.Value ?"空时显示的值":"不为空时显示的值" %>' alt="" />
  </a>
  </td>
  </tr>
  <tr>
  <td style="width: 360px; text-align: center;">
  <a href="#">
  <%#Eval("price")==System.DBNull.Value ?"空时显示的值":"不为空时显示的值"%>古币 </a>
  <asp:Label ID="Label4" runat="server" Text='<%#Eval("price")%>' Visible="false"></asp:Label>
  </td>
  </tr>
  <tr>
  <td style="width: 360px; text-align: center;">
  <a href="#">
  <%#Eval("userName")==System.DBNull.Value ?"空时显示的值":"不为空时显示的值"%>领先 </a>
  </td>
  </tr>
  <tr>
  <td style="width: 360px; text-align: center;">
  <asp:LinkButton ID="lbBight" runat="server">我要竞拍</asp:LinkButton>
  </td>
  </tr>
  </table>
  </div>
  </ItemTemplate>
  </asp:DataList>

------解决方案--------------------------------------------------------
在databound中判断最好
------解决方案--------------------------------------------------------
或者自定义sql:
select nvl(字段,"空") from table
------解决方案--------------------------------------------------------
HTML code
<!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">        <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">            <ItemTemplate>                <asp:Label ID="Name" runat="server"></asp:Label>            </ItemTemplate>        </asp:DataList>    </form></body>
  相关解决方案