Hi,
I have a page, which uses a master page template. In the content, I
have a formview control, and outside of that form view, I have a
Literal control. On initial load, the Literal control is available
for use. Inside the FormView, I have linkbuttons which serve as a
previous / next navigation, which changes the record displayed by the
formview. When one of these buttons is clicked, the reference to the
literal control is now null.
Any ideas? I would think the control should always be available since
its declared as a server tag.
With a code generated control, you are responsible for pulling the value
from viewstate and reconstituting the control. Since this is a literal,
it does not change, so you simply have to rebuild it with each hit on
the page.
The confusion here is Microsoft has a lot of wiring in the background
for controls dragged on a page. You don't see the wiring, so it does not
exist for you until you dig more deeply into the framework and CLR and
see what is going on. But, the plumbing is there, whether you see it or
not.
When you work with code, there is no plumbing automatically generated
for you. This makes you responsible for regenning the content.
What to do? There are a couple of options. If this is merely text, you
can use a label or similar. If JavaScript, you can use the client emit
method(s) - the ones with Register and Script in the title. If both, you
can use a combination.
Here is an example of a simple page with a panel and a button. It has a
routine to set up a literal on a panel and refresh it with a button
click.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindLiteralWithPanel();
}
protected void BindLiteralWithPanel()
{
LiteralControl literal = CreateLiteral();
pnlTest.Controls.Add(literal);
}
protected LiteralControl CreateLiteral()
{
string s = "<b>Test string</b><br />";
LiteralControl literal = new LiteralControl(s);
return literal;
}
protected void Button1_Click(object sender, EventArgs e)
{
//If you remove this, the literal disappears
BindLiteralWithPanel();
}
}
Hope this helps.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
*******************************************
| Think outside the box! |
*******************************************