Zero literal problem

I have a relay that should only show the value of a related field if it exists. After reading this post , I decided to do it by using a literal inside my relay and using the OnItemDatabound trigger to populate my literal, but my literal doesn't seem to be available from C # code and I don't understand why!

Here is the aspx page

    <asp:Repeater runat="server" ID="rpt_villaresults" OnItemDataBound="checkForChildren">
    <HeaderTemplate>

    </HeaderTemplate>
    <ItemTemplate>       
//.................MORE CODE HERE......................                           
<div class="sleeps"><h4>To Sleep</h4><h5><%#Eval("sleeps")%> <asp:Literal ID="sleepsChildrenLit" runat="server" /> </h5></div>
//.............MORE CODE HERE........................

      

And the code

public void checkForChildren(object sender, RepeaterItemEventArgs e)
{
    Literal childLit = e.Item.FindControl("sleepsChildrenLit") as Literal; 
    //this is null at runtime
    String str = e.Item.DataItem.ToString();
    if (e.Item.DataItem != null)
    {
        if (Regex.IsMatch(str, "[^0-9]"))
        {
            if (Convert.ToInt32(str) > 0)
            {
                childLit.Text = " + " + str;
            }
        }         
    }
}

      

0


source to share


3 answers


As you probably know when you say: as Literal

it can return values null

. If you make the right choice, you will get a runtime exception, which will give you more information about what is wrong and / or which element is causing you problems.

If you always expect "chilLit" to have a value and not check for null values, you should cast it to Literal using



Literal childLit = (Literal)e.Item.FindControl("sleepsChildrenLit");

      

+2


source


Well, with your current code, we don't know because e.Item.FindControl is returning null or because it wasn't Literal. This is why you should use cast instead of "as" if you are sure what type it really should be.

Change your code to:

Literal childLit = (Literal) e.Item.FindControl("sleepsChildrenLit");

      

and see what happens. If you get a cast exception, you know it because it is the wrong type. If you are still getting NRE, then FindControl returns null.



EDIT: Now, apart from that, let's see the code after it:

String str = e.Item.DataItem.ToString();
if (e.Item.DataItem != null)
{
    ...
}

      

If it e.item.DataItem

is null, then calling ToString () will throw an exception, so checking on the next line is pointless. I suspect you really want:

if (e.Item.DataItem != null)
{
    String str = e.Item.DataItem.ToString();
    ...
}

      

+2


source


The checkForChildren () OnItemDataBound event handler will also be called on the relay header element. But in this case e.Item.DataItem will be null. And of course FindControl () will return null as well, since you don't have a Literal control with id "sleepsChildrenLit" in the HeaderTemplate.

You can use the e.Item.ItemType property to check if the current item is the title of a FooterItem item or a "normal" item, for example:

if (e.Item.ItemType == ListItemType.Header)
{
...
}
else if (...)
{
...
}

      

+2


source







All Articles