Properly implement a webpart with postback?
Asked Answered
P

4

6

What I'm trying to do is to create a webpart that has a textbox where you can set the value of a literal (h2) on the webpart and a "save" button that posts back and then sets the literal accordingly. This works with one huge caveat; when the page loads after the postback the literal has not been changed. However if I log what is actually set in the literal it has the new value. Also if I reload the page again (F5) it displays correctly.

At first I figured it must be ViewState, so I disabled it for all controls. I verified that it is not being saved in the ViewState (decoded it). So ViewState is not saving the old value.

I'm using "CreateChildControls" to add my controls to the webpart. and the postback is handled by a simple event handler.

Any ideas?

For the record, I'm using MOSS 2007.

Phlogistic answered 17/2, 2009 at 9:46 Comment(2)
It would be easier to spot where the problem is if you showed some code.Octosyllabic
True, only problem is it's a rather large class.. But I've made it work more or less the way we need it to now.Phlogistic
P
3

Sounds like an ASP.NET event timing problem. Try calling EnsureChildControls() in the page load event. This ensures that your CreateChildControls() method is called and your controls are added to the page before the post back events are handled. If your controls are first added at the PreRender or Render stage it will be too late for them to pick up the post back data. You will then not see the change before the next page load.

Pawpaw answered 17/2, 2009 at 14:20 Comment(1)
All my controls are added during CreateChildControls(). And I've tried to call EnsureChildControls() in the OnLoad override, no luck.Phlogistic
G
1

Perhaps this blog post might help you to understand the life cycle of a webpart better and to solve your problem. http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/

Gigot answered 18/2, 2009 at 19:42 Comment(0)
R
0

You could always use an AJAX update panel, drop your literal control inside that and call an UDP.Update on the update panel. Also with your initial example check you have runat="server" on your literal control. You should be able to add a change to the Page_Load event and this should appear on the webpart.

Rattan answered 27/2, 2009 at 16:19 Comment(2)
Yeah, that couldv'e been an option, unfortunately the value in question here is dynamic; ie. the textbox may or may not be present in a webpart... ;)Phlogistic
Also; the literal is added via code (as are all controls in the webpart).Phlogistic
P
0

Here is a suggestion. It works for me anyway.

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Skaar.UI
{
    public class PostBackWebPart:WebPart
    {
        private Literal literal;
        private TextBox textBox;
        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);
            literal=new Literal();
            literal.Mode = LiteralMode.PassThrough;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
            Controls.Add(literal);
            textBox=new TextBox();
            textBox.AutoPostBack = true;
            Controls.Add(textBox);
            textBox.TextChanged += textBox_TextChanged;                                                                                                                                                                                                                                                                                                                                      
        }

        void textBox_TextChanged(object sender, System.EventArgs e)
        {
            literal.Text = string.Format("<h1>{0}</h1>", textBox.Text);
        }
    }
}
Perineurium answered 27/4, 2009 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.