ASP.NET User control Eval() DB.Null problem
I have a user control that contains a repeater. The repeater outputs either one or two columns based on the parameters used when an instance of开发者_开发问答 it is created.
If a value is not specified for the second column i.e only one column should be displayed, it doesn't seem to like it if a value hasn't been set.
I'm trying to output a string that is set in the user control code behind, not a data item.
I have the following aspx:
<%# Eval(Column2Data) == DBNull.Value ? "</tr> " : String.Format("<td>{0}</td></tr>", Eval(Column2Data))%>
If Column2Data
has a value, it works but if it doesn't the following error is displayed:
Value cannot be null.
Parameter name: expression
Any ideas why this isn't working?
Try this one
<% (Column2Data == null) ? "</tr>td></td></tr>", Eval(Column2Data))%>
If value is null I think you want to present empty td
This is what you're looking for:
<%# DBNull.Value.Equals(Eval(Column2Data)) ? "</tr> " : String.Format("<td>{0}</td></tr>", Eval(Column2Data))%>
Try this,
<%# Eval("Column2Data")==null ? "<tr><td>N.A</td></tr>"
: Eval("Column2Data","<tr><td>{0}</td></tr>")%>
This is the sample List,
public class Data
{
public int? Column2Data { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Data> list = new List<Data>()
{
new Data(){ Column2Data=10100},
new Data(){},
new Data(){ Column2Data=4000}
};
Repeater1.DataSource = list;
Repeater1.DataBind();
}
}
<%# (string.IsNullOrEmpty(Convert.ToString(Eval("Column2Data")))) ? "Value" :Eval("Column2Data") %>
精彩评论