现在的位置: 首页 > 综合 > 正文

GridView绑定技巧终结者

2012年04月02日 ⁄ 综合 ⁄ 共 2495字 ⁄ 字号 评论关闭


gridview列 数字、货币和日期 显示格式 
形式 语法 结果 注释 
数字 
{0:N2} 12.36   
数字 
{0:N0} 13   
货币 
{0:c2} $12.36   
货币 
{0:c4} $12.3656   
货币 
"¥{0:N2}" ¥12.36   
科学计数法 
{0:E3} 1.23E+001   
百分数 
{0:P} 12.25% P and p present the same. 
日期 
{0:D} 2006年11月25日   
日期 
{0:d} 2006-11-25   
日期 
{0:f} 2006年11月25日 10:30   
日期 
{0:F} 2006年11月25日 10:30:00   
日期 
{0:s} 2006-11-26 10:30:00   
时间 
{0:T} 10:30:00  

<1>绑定日期

 

<asp:BoundField DataField="AddDate" DataFormatString="{0:yyyy年MM月dd日}" HeaderText="添加日期" SortExpression="AddDate" />

 

 <2>绑定价格

 

<ItemTemplate><asp:TextBox Width="60" ID="Price" runat="server" Text='<%# Bind("Price","{0:n}") %>'></asp:TextBox></ItemTemplate>

 

<3>绑定货币

 

<ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("BookPrice", "{0:c}") %>'></asp:Label></ItemTemplate>

 

<4>带参数的链接

 

<asp:HyperLinkField Text="修改" DataNavigateUrlFields="catenaid" DataNavigateUrlFormatString="addBookCatena.aspx?edit={0}" HeaderText="修改" />

 

 

 

 <ItemTemplate><a runat="server" target="_blank" href='<%#"~/bookinfo.aspx?bookID="+Eval("bookID") %>' ><%# Eval("bookName"%></a></ItemTemplate>

<5>绑定bool变量

 <ItemTemplate><%# (bool)Eval("IsMain"? "<span style='color: Green'>YES</span>" : "<span tyle='color: Red'>NO</span>" %></ItemTemplate>

<6>鼠标提示

 <ItemTemplate><asp:Image ID="Image1" runat="server" AlternateText='<%# Eval("ImageURL") %>' ImageUrl='<%# Eval("ImageURL") %>' /> </ItemTemplate>

<7>函数绑定

<TD colspan="5" valign="middle" style="height: 24px">订单号:<%# Eval("orderID"%><%# DataFormat.isTg((bool)Eval("isTg"))%></TD>

函数定义如下:


public class DataFormat
{
    
public static string isTg(bool isTg)
    
{
        
string TgTxt = "";
        
if (isTg)
        
{
            TgTxt 
= "<font color=red>(此为团购定单)</font>";
        }

        
return TgTxt;
    }

}

<8>绑定JS

 <asp:TemplateField>
       
<HeaderTemplate><input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" /></HeaderTemplate>
       
<ItemTemplate><asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
 
</asp:TemplateField>

 


// JScript 文件
//
checkbox全选
function SelectAllCheckboxes(spanChk){

   
// Added as ASPX uses SPAN for checkbox
   var oItem = spanChk.children;
   
var theBox= (spanChk.type=="checkbox"? 
        spanChk : spanChk.children.item[
0];
   xState
=theBox.checked;
   elm
=theBox.form.elements;

   
for(i=0;i<elm.length;i++)
     
if(elm[i].type=="checkbox" && 
              elm[i].id
!=theBox.id)
     
{
       
//elm[i].click();
       if(elm[i].checked!=xState)
         elm[i].click();
       
//elm[i].checked=xState;
     }

 }

抱歉!评论已关闭.