C# 使用eval在gridview列中绑定数据
我有一个网格,其中包含一列用于显示国家名称。我需要将该列中的值显示为contrycode国家名称的前10个字母(在印度)。我在项目模板中使用Eval函数进行了尝试:C# 使用eval在gridview列中绑定数据,c#,asp.net,C#,Asp.net,我有一个网格,其中包含一列用于显示国家名称。我需要将该列中的值显示为contrycode国家名称的前10个字母(在印度)。我在项目模板中使用Eval函数进行了尝试: <asp:TemplateField> <ItemTemplate> <asp:Label ID="CountryNameLabe" runat="server" Text='<%# Eval("CorporateAddressCountry").SubString(0,6) %&
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="CountryNameLabe" runat="server" Text='<%# Eval("CorporateAddressCountry").SubString(0,6) %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
但它显示出错误。
我可以在eval中使用自定义函数吗?
请帮助您可以使用三元运算符
?
:
<asp:Label ID="CountryNameLabel" runat="server"
Text='<%# Eval("CorporateAddressCountry").ToString().Length <= 10 ? Eval("CorporateAddressCountry") : Eval("CorporateAddressCountry").ToString().Substring(0,10) %>' >
</asp:Label>
您可以使用三元运算符
?
:
<asp:Label ID="CountryNameLabel" runat="server"
Text='<%# Eval("CorporateAddressCountry").ToString().Length <= 10 ? Eval("CorporateAddressCountry") : Eval("CorporateAddressCountry").ToString().Substring(0,10) %>' >
</asp:Label>
我是用电脑做的
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label countryNameLabel = (Label)e.Row.FindControl("CountryNameLabel");
countryNameLabel.ToolTip = countryNameToolTip(countryNameLabel.Text);
countryNameLabel.Text = countryNameDisplay(countryNameLabel.Text);
}
}
protected string countryNameDisplay(string key)
{
CustomerBusinessProvider business = new CustomerBusinessProvider();
string country = business.CountryName(key);
country = key + "-" + country;
if (country.Length > 10)
{
country = country.Substring(0, 10) + "...";
}
return country;
}
protected string countryNameToolTip(string key)
{
CustomerBusinessProvider business = new CustomerBusinessProvider();
return business.CountryName(key);
}
我是用电脑做的
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label countryNameLabel = (Label)e.Row.FindControl("CountryNameLabel");
countryNameLabel.ToolTip = countryNameToolTip(countryNameLabel.Text);
countryNameLabel.Text = countryNameDisplay(countryNameLabel.Text);
}
}
protected string countryNameDisplay(string key)
{
CustomerBusinessProvider business = new CustomerBusinessProvider();
string country = business.CountryName(key);
country = key + "-" + country;
if (country.Length > 10)
{
country = country.Substring(0, 10) + "...";
}
return country;
}
protected string countryNameToolTip(string key)
{
CustomerBusinessProvider business = new CustomerBusinessProvider();
return business.CountryName(key);
}
@PanagiotisKanavos:它在那里,只是因为里面有html标记而没有显示。@PanagiotisKanavos:它在那里,只是因为里面有html标记而没有显示。这假设他的错误与字符串不够长有关。如果他的字段正在生成
DBNull
,这仍然会抛出异常。我想将它与另一个字段的国家/地区代码合并,例如:美国-美国-美国Ame…@JoelEtherton:是的,但如果不是这样,它还提供了一种调试代码的方法,以查看异常的实际原因。@ROBIN:如果要将其与另一个字段组合,请首先在select,f.e.SQL Server:select CountryCode+'-'+CorporateAddressCountry AS CountryInfo FROM TblCountry
。这是最有效、最健壮的方法,它还允许在GridView中按该字段进行排序。但是您也可以使用我的代码隐藏方法来组合这些字段。这假设他的错误与字符串不够长有关。如果他的字段正在生成DBNull
,这仍然会抛出异常。我想将它与另一个字段的国家/地区代码合并,例如:美国-美国-美国Ame…@JoelEtherton:是的,但如果不是这样,它还提供了一种调试代码的方法,以查看异常的实际原因。@ROBIN:如果要将其与另一个字段组合,请首先在select,f.e.SQL Server:select CountryCode+'-'+CorporateAddressCountry AS CountryInfo FROM TblCountry
。这是最有效、最健壮的方法,它还允许在GridView中按该字段进行排序。但您也可以使用我的代码隐藏方法来组合这些字段。