LoadPostData not firing

Discussion in 'ASP .Net Building Controls' started by John Teague, Jul 26, 2005.

  1. John Teague

    John Teague Guest

    I am trying to create a control that inherits from ListControl. I am
    implementing the IPostBackDataHandler, but the LoadPostData method is never
    called.

    I've read that in addition to implementing these methods, one of the html
    tags must have a name = control's uniqueID. However, I've done this with no
    success and have seen others that do not set the name property anywere.

    Any help will be greatly appreciated.
    Thanks,
    John
     
    John Teague, Jul 26, 2005
    #1
    1. Advertising

  2. John Teague

    sam Guest

    Make sure when you test this control that you add it to the page in the
    OnInit() method. You can also try adding it in PreRender() but
    OnInit() is prefered.
     
    sam, Jul 27, 2005
    #2
    1. Advertising

  3. John Teague

    John Teague Guest

    I have the control tag on the aspx page. I'm not dynamically adding it.
    What needs to be in Init?

    "sam" wrote:

    > Make sure when you test this control that you add it to the page in the
    > OnInit() method. You can also try adding it in PreRender() but
    > OnInit() is prefered.
    >
    >
     
    John Teague, Jul 27, 2005
    #3
  4. John Teague

    Teemu Keiski Guest

    Shouldn't be needed.

    Can you show code for the control if it's not too long/complex to post? How
    does it render itself?

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "John Teague" <> wrote in message
    news:...
    >I have the control tag on the aspx page. I'm not dynamically adding it.
    > What needs to be in Init?
    >
    > "sam" wrote:
    >
    >> Make sure when you test this control that you add it to the page in the
    >> OnInit() method. You can also try adding it in PreRender() but
    >> OnInit() is prefered.
    >>
    >>
     
    Teemu Keiski, Jul 27, 2005
    #4
  5. John Teague

    John Teague Guest

    I have tried several different ways of rendering the control with the same
    results. This was my first incantation.

    private CheckBox ControlToRepeat(int index)
    {
    CheckBox control;
    if(this.SurveyResponseType == ResponseType.CheckBox)
    control = new CheckBox();
    else
    {
    control = new RadioButton();
    }
    control.Page = this.Page;
    control.ID = this.ClientID + "_" + index.ToString();
    control.Attributes["value"] = this.Items[index].Value;
    control.AutoPostBack = this.AutoPostBack;
    control.TabIndex = this.TabIndex;
    control.Enabled = this.Enabled;
    return control;

    }

    protected override void Render(HtmlTextWriter output)
    {
    testLabel.Text = "Label";
    testLabel.RenderControl(output);
    RepeatInfo info = new RepeatInfo();
    Style listStyle = base.ControlStyleCreated ? base.ControlStyle : null;
    short tIndex = this.TabIndex;
    Table t = new Table();
    TableRow r1 = new TableRow();
    TableRow r2 = new TableRow();
    for(int i = 0; i<this.Items.Count; i++)
    {

    TableCell textCell = new TableCell();
    textCell.Text = this.Items.Text;
    r1.Cells.Add(textCell);



    TableCell valueCell = new TableCell();
    valueCell.Controls.Add(ControlToRepeat(i));
    r2.Cells.Add(valueCell);

    }
    t.Rows.Add(r1);
    t.Rows.Add(r2);
    t.RenderControl(output);

    }

    I have stuff in the LoadPostData method too, but it's irreleveant because
    the method never gets called. I tried overriding the ControlCollection and
    adding them
    my stuff that way and I've tried using the render methods for each of these
    objects and sending them directly to the HTMLTextWriter object.

    Thanks for the help on this.
    "Teemu Keiski" wrote:

    > Shouldn't be needed.
    >
    > Can you show code for the control if it's not too long/complex to post? How
    > does it render itself?
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    > "John Teague" <> wrote in message
    > news:...
    > >I have the control tag on the aspx page. I'm not dynamically adding it.
    > > What needs to be in Init?
    > >
    > > "sam" wrote:
    > >
    > >> Make sure when you test this control that you add it to the page in the
    > >> OnInit() method. You can also try adding it in PreRender() but
    > >> OnInit() is prefered.
    > >>
    > >>

    >
    >
    >
     
    John Teague, Jul 27, 2005
    #5
  6. John Teague

    sam Guest

    Can you put a breakpoint on the Render() or Init() method and look at
    Request.Form.Keys and make sure there is a key in there that matches
    the UniqueID of your control. Also make sure your control is contained
    in a Form tag.
     
    sam, Jul 28, 2005
    #6
  7. John Teague

    Teemu Keiski Guest

    I see that you are creating and adding the child CheckBoxes within Render.
    That's one problematic part if you want to use them like child controls
    (would be needed to instantiate like in databinding) however it's not the
    problem in this case.

    Problem is that CheckBox's UniqueID is the one which is present in
    Request.Form collection but your container control or custom list control is
    the one expecting postback data handling. So to say, it's not your custom
    list's UniqueID in Request.Form collection and therefore its LoadPostData
    won't get called (Page runs FindControl with the uniqueid and if a control
    is found, tries to cast it to IPostBackDataHandler)

    There's a mechanism to ensure that LoadPostData is called despite that
    control's UniqueID is not present in post data collection (Request.Form).
    The control needs to of course still implement IPostBackDataHandler
    interface and register itself to the Page to require postback data handling.
    This happens by calling Page.RegisterRequiresPostBack method. And a good
    place for it is in overridden OnPreRender method.

    So it could look like this

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);
    if(Page != null)
    Page.RegisterRequiresPostBack(this);
    }

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "John Teague" <> wrote in message
    news:...
    >I have tried several different ways of rendering the control with the same
    > results. This was my first incantation.
    >
    > private CheckBox ControlToRepeat(int index)
    > {
    > CheckBox control;
    > if(this.SurveyResponseType == ResponseType.CheckBox)
    > control = new CheckBox();
    > else
    > {
    > control = new RadioButton();
    > }
    > control.Page = this.Page;
    > control.ID = this.ClientID + "_" + index.ToString();
    > control.Attributes["value"] = this.Items[index].Value;
    > control.AutoPostBack = this.AutoPostBack;
    > control.TabIndex = this.TabIndex;
    > control.Enabled = this.Enabled;
    > return control;
    >
    > }
    >
    > protected override void Render(HtmlTextWriter output)
    > {
    > testLabel.Text = "Label";
    > testLabel.RenderControl(output);
    > RepeatInfo info = new RepeatInfo();
    > Style listStyle = base.ControlStyleCreated ? base.ControlStyle : null;
    > short tIndex = this.TabIndex;
    > Table t = new Table();
    > TableRow r1 = new TableRow();
    > TableRow r2 = new TableRow();
    > for(int i = 0; i<this.Items.Count; i++)
    > {
    >
    > TableCell textCell = new TableCell();
    > textCell.Text = this.Items.Text;
    > r1.Cells.Add(textCell);
    >
    >
    >
    > TableCell valueCell = new TableCell();
    > valueCell.Controls.Add(ControlToRepeat(i));
    > r2.Cells.Add(valueCell);
    >
    > }
    > t.Rows.Add(r1);
    > t.Rows.Add(r2);
    > t.RenderControl(output);
    >
    > }
    >
    > I have stuff in the LoadPostData method too, but it's irreleveant because
    > the method never gets called. I tried overriding the ControlCollection
    > and
    > adding them
    > my stuff that way and I've tried using the render methods for each of
    > these
    > objects and sending them directly to the HTMLTextWriter object.
    >
    > Thanks for the help on this.
    > "Teemu Keiski" wrote:
    >
    >> Shouldn't be needed.
    >>
    >> Can you show code for the control if it's not too long/complex to post?
    >> How
    >> does it render itself?
    >>
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, AspInsider
    >> Finland, EU
    >> http://blogs.aspadvice.com/joteke
    >>
    >> "John Teague" <> wrote in message
    >> news:...
    >> >I have the control tag on the aspx page. I'm not dynamically adding it.
    >> > What needs to be in Init?
    >> >
    >> > "sam" wrote:
    >> >
    >> >> Make sure when you test this control that you add it to the page in
    >> >> the
    >> >> OnInit() method. You can also try adding it in PreRender() but
    >> >> OnInit() is prefered.
    >> >>
    >> >>

    >>
    >>
    >>
     
    Teemu Keiski, Jul 28, 2005
    #7
  8. John Teague

    John Teague Guest

    Ok, that must have been it. Though I'm really not sure how I fixed it?
    My control renders either a RadioButton or a CheckBox depending on a
    property set. I added UniqueID as the GroupName of the RadioButton and it is
    now firing the LoadPostData. Even when it is a checkbox.

    The debugging suggestion put me in the right direction. Thanks

    "sam" wrote:

    > Can you put a breakpoint on the Render() or Init() method and look at
    > Request.Form.Keys and make sure there is a key in there that matches
    > the UniqueID of your control. Also make sure your control is contained
    > in a Form tag.
    >
    >
     
    John Teague, Jul 28, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    2
    Views:
    4,541
  2. Lin
    Replies:
    0
    Views:
    1,943
  3. Nathan Sokalski
    Replies:
    0
    Views:
    317
    Nathan Sokalski
    Aug 23, 2008
  4. Nathan Sokalski
    Replies:
    0
    Views:
    422
    Nathan Sokalski
    Aug 26, 2008
  5. John Bailey

    LoadPostData not firing if control is in a formview

    John Bailey, Oct 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    185
    John Bailey
    Oct 23, 2005
Loading...

Share This Page