C# 我得到NullReferenceException
我在填充Repeater时得到NullReferenceException。一些产品功能为空。我在使用UrlDecode()时使用IsNullControl()方法,在使用kill()方法避免异常。但我仍然得到错误C# 我得到NullReferenceException,c#,asp.net,repeater,nullreferenceexception,urldecode,C#,Asp.net,Repeater,Nullreferenceexception,Urldecode,我在填充Repeater时得到NullReferenceException。一些产品功能为空。我在使用UrlDecode()时使用IsNullControl()方法,在使用kill()方法避免异常。但我仍然得到错误 <asp:Repeater ID="rptProducts" runat="server"> <ItemTemplate> <div> <%# Ev
<asp:Repeater ID="rptProducts" runat="server">
<ItemTemplate>
<div>
<%# Eval("ProductName")%>
</div>
<div>
<%# kill(Server.UrlDecode(IsNullControl(Eval("ProductFeature").ToString())))%>
</div>
</ItemTemplate>
</asp:Repeater>
try
{
ProductsDataContext pdc = new ProductsDataContext();
var query = from p in pdc.Products
select p;
rptProducts.DataSource = query;
rptProducts.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
public static string kill(string val)
{
val = val.Replace("<ul>", " ");
val = val.Replace("<li>", " ");
val = val.Replace("</li>", "<br/>");
val = val.Replace("</ul>", " ");
return val.ToString();
}
public static string IsNullControl(string val)
{
string space = " ";
if (string.IsNullOrEmpty(val))
{
val = space;
}
return space;
}
尝试
{
ProductsDataContext pdc=新产品sDataContext();
var query=来自pdc.Products中的p
选择p;
rptProducts.DataSource=查询;
rptProducts.DataBind();
}
捕获(例外情况除外)
{
响应。写入(例如消息);
}
公共静态字符串终止(字符串val)
{
val=val.Replace(“”,”);
val=val.Replace(“- ”,”);
val=val.Replace(“
”,“
”);
val=val.Replace(“
”,”);
返回值ToString();
}
公共静态字符串IsNullControl(字符串val)
{
字符串空格=”;
if(string.IsNullOrEmpty(val))
{
val=空间;
}
返回空间;
}
在检查此代码片段中的空值之前,您首先将字段转换为字符串-->Eval(“ProductFeature”)。ToString()
在检查此代码片段中的空值之前,您首先将字段转换为字符串-->Eval(“ProductFeature”)。ToString()
如下检查空值
<%#kill(Server.UrlDecode(Eval("ProductFeature")?? String.Empty))%>
var query = from p in pdc.Products
select p
where p!= null;
检查空值,如下所示
<%#kill(Server.UrlDecode(Eval("ProductFeature")?? String.Empty))%>
var query = from p in pdc.Products
select p
where p!= null;
测试
IsNullControl
的结果。这是您期望的吗?IsNullControl
的测试结果可能重复。这是你期望的吗?可能是