Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > LoadPostData not firing

Reply
Thread Tools

LoadPostData not firing

 
 
John Teague
Guest
Posts: n/a
 
      07-26-2005
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
 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      07-26-2005
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.

 
Reply With Quote
 
 
 
 
John Teague
Guest
Posts: n/a
 
      07-27-2005
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.
>
>

 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-27-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>>
>>



 
Reply With Quote
 
John Teague
Guest
Posts: n/a
 
      07-27-2005
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[i].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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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.
> >>
> >>

>
>
>

 
Reply With Quote
 
sam
Guest
Posts: n/a
 
      07-28-2005
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.

 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-28-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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[i].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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >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.
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
John Teague
Guest
Posts: n/a
 
      07-28-2005
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.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LoadPostData and postCollection not returning the updated values for DropDownLists Nathan Sokalski ASP .Net 0 08-26-2008 01:46 AM
LoadPostData and PostCollection not returning the updated values for DropDownLists Nathan Sokalski ASP .Net 0 08-23-2008 10:26 PM
LoadPostData not firing if control is in a formview John Bailey ASP .Net Web Controls 0 10-23-2005 05:07 PM
Why do LoadPostData and RaisePostDataChangedEvent not be executed? Lin ASP .Net 0 10-17-2004 11:52 AM
LoadPostData not being called at the right time for controls dynamically added at page load Sam ASP .Net 2 04-26-2004 07:19 AM



Advertisments