I am trying to get an asp: literal to work but apparently I am doing something wrong because I am getting an
Object Referece not set to an instance of the Object
error
This is my code:
in the ascx page:
<span class="span1">
<asp:Literal ID="litFile" runat="server"></asp:Literal>
<strong><asp:Literal ID="litFile2" runat="server"></asp:Literal></strong>
</span>
and
in the ascx.cs page:
protected void _ItemBound(object sender, RepeaterItemEventArgs e)
{
((Literal)开发者_JS百科e.Item.FindControl("litFile")).Text = "a";
}
Any idea please?
C
We need more code to know for sure, but what this looks like to me is that the literal is actually outside the repeater that's firing the _ItemBound
method. Otherwise, e.Item.FindControl
should be able to find it. Make sure litFile
is inside the ItemTemplate
in your repeater control, rather than elsewhere in the page.
You shouldn't need to cast it, nor should you need to search for it.
Try just:
litFile.Text = "a";
If this doesn't work, we will need to see more code to work out what's going on.
Since your using FindControl I assume your in a repeater or something, so try this
Literal litFile = repeaterName.FindControl("litFile");
litFile.text = "a";
I code in VB so sorry if my syntax is off.
精彩评论