Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > UpdatePanel and RecreateChildControls

Reply
Thread Tools

UpdatePanel and RecreateChildControls

 
 
jeljeljel
Guest
Posts: n/a
 
      09-20-2007
I have a CompositeControl (called SimplePager) that creates an
UpdatePanel in its CreateChildControls override. The CompositeControl
uses two LinkButtons to control the forward/backward state. W/out the
use of the UpdatePanel it works just great. However, as a learning
exercise I want to AJAXify the control. I am placing the entire
control in an UpdatePanel. During the link click event handler, I
make a call to RecreateChildControls() which throws an exception.

This is so close to working. Can someone suggest how to accomplish
what I am trying to do with an UpdatePanel?

My class is copied below.

Thanks,
John


public class SimplePager : CompositeControl
{
private int _currentRecord;
private int _totalRecords;

public SimplePager(int totalRecords)
{
_totalRecords = totalRecords;
_currentRecord = 1;
}

protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);

EnsureChildControls();

base.OnInit(e);
}

protected override void LoadControlState(object savedState)
{
int[] state;

state = (int[])savedState;

_currentRecord = state[0];
_totalRecords = state[1];
}

protected override object SaveControlState()
{
int[] state;

state = new int[] { _currentRecord, _totalRecords };

return state;
}

protected override void CreateChildControls()
{
HtmlGenericControl div;
LinkButton linkButton;
UpdatePanel updatePanel;
Label label;

div = new HtmlGenericControl("div");
Controls.Add(div);

updatePanel = new UpdatePanel();
updatePanel.ID = this.ID + "BWPagerUpdatePanel";
updatePanel.ChildrenAsTriggers = true;
updatePanel.UpdateMode =
UpdatePanelUpdateMode.Conditional;
div.Controls.Add(updatePanel);

linkButton = new LinkButton();
linkButton.Text = "<< prev";
linkButton.ID = "prevPage";
linkButton.Click += new EventHandler(linkButton_Click);

updatePanel.ContentTemplateContainer.Controls.Add( linkButton);

linkButton = new LinkButton();
linkButton.Text = "next >>";
linkButton.ID = "nextPage";
linkButton.Click += new EventHandler(linkButton_Click);

updatePanel.ContentTemplateContainer.Controls.Add( linkButton);

label = new Label();
label.ID = "SimplePagerTextBox";
label.Text = String.Format("&nbsp;&nbsp;Page {0} of {1}",
_currentRecord, _totalRecords);
updatePanel.ContentTemplateContainer.Controls.Add( label);

base.CreateChildControls();
}

private void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = sender as LinkButton;

if (PageChange != null)
{
switch (linkButton.ID)
{
case "prevPage":
if (_currentRecord > 1)
_currentRecord--;
break;

case "nextPage":
if (_currentRecord < _totalRecords)
_currentRecord++;
break;
}

PageChange(this, _currentRecord);

RecreateChildControls();
}
}

public int CurrentRecord
{
get
{
return _currentRecord;
}
}

public delegate void PageChangeEvent(object sender, int
newPage);
public event PageChangeEvent PageChange;
}

 
Reply With Quote
 
 
 
 
Phil H
Guest
Posts: n/a
 
      09-22-2007
Dear jeljeljel

You make a call to RecreateChildControls() but no such method exists
in the code you present.

It's difficult to diagnose why a method is raising an exception if its
code is not shown. Or could that be the cause of the problem?


On 20 Sep, 18:17, jeljeljel <(E-Mail Removed)> wrote:
> I have a CompositeControl (called SimplePager) that creates an
> UpdatePanel in its CreateChildControls override. The CompositeControl
> uses two LinkButtons to control the forward/backward state. W/out the
> use of the UpdatePanel it works just great. However, as a learning
> exercise I want to AJAXify the control. I am placing the entire
> control in an UpdatePanel. During the link click event handler, I
> make a call to RecreateChildControls() which throws an exception.
>
> This is so close to working. Can someone suggest how to accomplish
> what I am trying to do with an UpdatePanel?
>
> My class is copied below.
>
> Thanks,
> John
>
> public class SimplePager : CompositeControl
> {
> private int _currentRecord;
> private int _totalRecords;
>
> public SimplePager(int totalRecords)
> {
> _totalRecords = totalRecords;
> _currentRecord = 1;
> }
>
> protected override void OnInit(EventArgs e)
> {
> Page.RegisterRequiresControlState(this);
>
> EnsureChildControls();
>
> base.OnInit(e);
> }
>
> protected override void LoadControlState(object savedState)
> {
> int[] state;
>
> state = (int[])savedState;
>
> _currentRecord = state[0];
> _totalRecords = state[1];
> }
>
> protected override object SaveControlState()
> {
> int[] state;
>
> state = new int[] { _currentRecord, _totalRecords };
>
> return state;
> }
>
> protected override void CreateChildControls()
> {
> HtmlGenericControl div;
> LinkButton linkButton;
> UpdatePanel updatePanel;
> Label label;
>
> div = new HtmlGenericControl("div");
> Controls.Add(div);
>
> updatePanel = new UpdatePanel();
> updatePanel.ID = this.ID + "BWPagerUpdatePanel";
> updatePanel.ChildrenAsTriggers = true;
> updatePanel.UpdateMode =
> UpdatePanelUpdateMode.Conditional;
> div.Controls.Add(updatePanel);
>
> linkButton = new LinkButton();
> linkButton.Text = "<< prev";
> linkButton.ID = "prevPage";
> linkButton.Click += new EventHandler(linkButton_Click);
>
> updatePanel.ContentTemplateContainer.Controls.Add( linkButton);
>
> linkButton = new LinkButton();
> linkButton.Text = "next >>";
> linkButton.ID = "nextPage";
> linkButton.Click += new EventHandler(linkButton_Click);
>
> updatePanel.ContentTemplateContainer.Controls.Add( linkButton);
>
> label = new Label();
> label.ID = "SimplePagerTextBox";
> label.Text = String.Format("&nbsp;&nbsp;Page {0} of {1}",
> _currentRecord, _totalRecords);
> updatePanel.ContentTemplateContainer.Controls.Add( label);
>
> base.CreateChildControls();
> }
>
> private void linkButton_Click(object sender, EventArgs e)
> {
> LinkButton linkButton = sender as LinkButton;
>
> if (PageChange != null)
> {
> switch (linkButton.ID)
> {
> case "prevPage":
> if (_currentRecord > 1)
> _currentRecord--;
> break;
>
> case "nextPage":
> if (_currentRecord < _totalRecords)
> _currentRecord++;
> break;
> }
>
> PageChange(this, _currentRecord);
>
> RecreateChildControls();
> }
> }
>
> public int CurrentRecord
> {
> get
> {
> return _currentRecord;
> }
> }
>
> public delegate void PageChangeEvent(object sender, int
> newPage);
> public event PageChangeEvent PageChange;
> }



 
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
Triggering an UpdatePanel with a trigger located outside of the UpdatePanel Nathan Sokalski ASP .Net 1 06-15-2009 06:23 PM
CompositeControls and RecreateChildControls() jeljeljel ASP .Net Building Controls 1 09-28-2007 05:25 AM
refresh an updatePanel in an other updatePanel fran_j_diaz@yahoo.fr ASP .Net 3 08-08-2007 06:30 AM
updatepanel and javascript =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 0 10-25-2006 08:47 PM
Master Pages and Ajax UpdatePanel =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 0 10-06-2006 09:10 PM



Advertisments