English 中文(简体)
我得到NullReference例外
原标题:I get NullReferenceException

i 使用 UrlDecode () 和 kill () 方法来避免例外 。 但我还是有错误 。

   <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;
    }
问题回答

您正在先将字段转换为字符串, 然后再检查代码碎片中的空- (gt;) Eval (“ ProductionFetary”) 。 ToString ()

无效复选( 如下)

<%#kill(Server.UrlDecode(Eval("ProductFeature")?? String.Empty))%>

或您可以将查询更改如下

 var query = from p in pdc.Products
             select p
             where p!= null;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!