Child OnPreRender method not being called.

M

Matt T.

I am creating a new control that inherits from the asp:calendar
control so I can add some additional functionality. The way I decided
to do this is to build my controls and render them to an intermediary
HtmlTextWriter, next I render the Calendar control itself to an
intermediary, and finally I use a regex to replace the Html in the
Calendar that I want to replace.

This all works fine, except for one thing. One of the controls I am
using has an overridden OnPreRender method, which is used to register
some client side script. However, this is not being called. I have
looked at CreateChildControls and some other stuff, but there are not
children of my control, they are children of the TableRow and
TableCell controls I am creating. I tried moving the creation to my
controls OnPreRender method, but the client script was still not
registered.

Anyone have any ideas how to make the OnPreRender methods fire without
adding them as child controls?

Here's the (abbreviated) code as it stands now. The AcornDropDownList
is the control with the OnPreRender method that is not being called.

protected override void Render(HtmlTextWriter writer)
{
if (base.ShowTitle)
{
if (isBrowserIE)
{
StringWriter titleWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(titleWriter);
this.CreateCalendarTitle().RenderControl(htmlWriter);

StringWriter baseWriter = new StringWriter();
HtmlTextWriter baseHtmlWriter = new HtmlTextWriter(baseWriter);
base.Render(baseHtmlWriter);

string html = Regex.Replace(baseWriter.ToString(),
@"(?<=<table[^>]*>.*?)<table[^>]*>.*?</table>",
titleWriter.ToString(),
RegexOptions.Singleline);

writer.Write(html);
}
}
else
{
base.Render(writer);
}
}

private WebControl CreateCalendarTitle()
{
Table title = new Table();

TableCell monthYear = new TableCell();
monthYear.Controls.Add(this.CreateMonthDropDownList());

TableRow row = new TableRow();
row.Cells.Add(monthYear);

title.Rows.Add(row);

return title;
}

private WebControl CreateMonthDropDownList()
{
AcornDropDownList list = new AcornDropDownList();

string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;

for (int i = 0; i < months.Length - 1; i++)
{
int visibleMonth = base.VisibleDate.Month;

AcornListItem item = new AcornListItem();
item.Text = months.PadRight(10, System.Convert.ToChar(160));
item.Selected = (i + 1 == visibleMonth);

DateTime date = base.VisibleDate.AddMonths(i + 1 - visibleMonth);

item.Command = Page.GetPostBackClientEvent(this,
this.GetPostBackArgument(date));

list.Items.Add(item);
}

return list;
}
 
S

skilesare

OnPreRender should be an event delegate so you should just be able to call
it from your new control. For example. If you need it to have been called
in you render method call MyChildControl.OnPreRender(e). I believe that e
can be null. This should call it and execute the code for that child object
object.

The fact that the object doesn't have a reference to the form that the
script will affect might cause some problems, but give it a try and see what
happens.

-Austin
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top