使用eval在GridView的列数据绑定 [英] Data binding in gridview column using eval

查看:142
本文介绍了使用eval在GridView的列数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含显示countrynames列HAVA我格。我需要在该列显示值contry code-第一个国名(在印度)。我试图在项模板使用eval函数用10个字母:

I hava grid containing a column for displaying countrynames. I need to display value in that column as contrycode-first 10 letters of country name (in-India) .I tried it using Eval functions with in the item template:

<asp:TemplateField>
  <ItemTemplate>
      <asp:Label ID="CountryNameLabe" runat="server" Text='<%# Eval("CorporateAddressCountry").SubString(0,6) %>' ></asp:Label>
  </ItemTemplate>
</asp:TemplateField>

但它显示错误。
我可以使用自定义功能的eval?
请大家帮忙

But it shows error. Can i use custom functions in eval? please help

推荐答案

您可以使用三元运算

<asp:Label ID="CountryNameLabel" runat="server" 
    Text='<%# Eval("CorporateAddressCountry").ToString().Length <= 10 ? Eval("CorporateAddressCountry") : Eval("CorporateAddressCountry").ToString().Substring(0,10) %>' >
</asp:Label>

另外,在我看来更可读的,方法是使用GridView的的 的RowDataBound 事件:

protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        var row = (DataRowView) e.Row.DataItem;
        var CountryNameLabel = (Label) e.Row.FindControl("CountryNameLabel");
        String CorporateAddressCountry = (String) row["CorporateAddressCountry"];
        CountryNameLabel.Text = CorporateAddressCountry.Length <= 10 
                               ? CorporateAddressCountry
                               : CorporateAddressCountry.Substring(0, 10);
    }
}

这篇关于使用eval在GridView的列数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆