LoadPostData not firing

J

John Teague

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
 
S

sam

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

John Teague

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

Teemu Keiski

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?
 
J

John Teague

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

sam

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

Teemu Keiski

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 said:
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 said:
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
 
J

John Teague

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
 

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top