Control is null on postback?

A

Andy

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.

Thanks
Andy
 
G

Gregory A. Beamer

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.
 
A

Andy

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();
    }

}

Hi,

Thanks for the feedback, but I think there is a misunderstanding. I'm
not creating the Literal control in code; it's part of the markup in
the aspx page, just like if I dragged the control onto the page using
the designer. This is why I'm confused as to the behavior... The page
is something like this:

<asp:Content>
<asp:Literal id="mylit" />
<asp:FormView>
<ItemTemplate>
<asp:DropDownList AutoPostBack="true"/>
</ItemTemplate>
</asp:FormView>
</asp:Content>

So when the dropdown does its autopost back to run some code for its
OnSelectedIndexChanged event, the literal mylit is null.

Thanks
Andy
 
G

Gregory A. Beamer

Hi,

Thanks for the feedback, but I think there is a misunderstanding. I'm
not creating the Literal control in code; it's part of the markup in
the aspx page, just like if I dragged the control onto the page using
the designer. This is why I'm confused as to the behavior... The page
is something like this:

<asp:Content>
<asp:Literal id="mylit" />
<asp:FormView>
<ItemTemplate>
<asp:DropDownList AutoPostBack="true"/>
</ItemTemplate>
</asp:FormView>
</asp:Content>

So when the dropdown does its autopost back to run some code for its
OnSelectedIndexChanged event, the literal mylit is null.


<asp:Literal id="mylit" runat="server" />

That is the first thing I would do. If you are simply filling this with
text, instead of HTML markup, I would go with a label instead:

<asp:Label id="mylit" runat="server" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top