Saturday, March 24, 2012

Literal nested in UpdatePanel error: "Object reference not set to an instance of an object

I am trying to set the content of a literal which is nested inside an UpdatePanel. But when I try to reference the Literal control, I get this message: "Object reference not set to an instance of an object." This despite the fact that the name shows in Intellisense. Actually, not entirely true. It sometimes isn't showing in Intellisense, for reasons I can't fathom. But I'm not getting a compilation error, so I assume it does recognise the name, but for some reason the object is null. Here's the code:

 <asp:UpdatePanel runat="server" ID="UpdatePanel1" EnableViewState ="true" Visible="true" RenderMode="Block" UpdateMode="Conditional"> <asp:ContentTemplate> <div style="width:350px; height:400px" class="newscontent"> <asp:Literal runat="server" ID="litContent"></asp:Literal> </div> </asp:ContentTemplate> </asp:UpdatePanel>

C#:

protected void Refresh(Object Sender, RepeaterCommandEventArgs e) {if (e.CommandName=="GetContent") {int id = Convert.ToInt32((e.CommandArgument)); ObjectModel.News item =new ObjectModel.News(id); litContent.Text = item.Content; litContent.Text += DisplayDocuments(id); UpdatePanel1.Update(); } }
The Refresh function is triggered by the user clicking on a LinkButton in a repeater, but this is irrelevant to the problem, since I get the same error message if I put a simple line of code in the page load function to set the text property on the the literal.

just yesterday got same problem with accordion control and textbox

spend about 3 hours here how it goes :UpdatePanel1 onpage load or on page init try this code

///////////////////////////////////////////////////////////////////////////

Literal litContent=newLiteral();

litContent= UpdatePanel1.FindControl ("litContent") asLiteral;

////////////////////////////////////////////////////


Sol90046:

just yesterday got same problem with accordion control and textbox

spend about 3 hours here how it goes :UpdatePanel1 onpage load or on page init try this code

///////////////////////////////////////////////////////////////////////////

Literal litContent=newLiteral();

litContent= UpdatePanel1.FindControl ("litContent") asLiteral;

////////////////////////////////////////////////////

Thank you! This worked as far as eliminating my error message went. But even though the code now appears to be doing what it is told, i.e., I can step through it in debug mode and the text property of the Literal is being set to the correct content, the update panel is not displaying the results. It also appears to be doing a full page refresh rather than a partial one. I do have a scriptmanager on the page with EnablePartialRendering set to true.

No comments:

Post a Comment