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;
}
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;
}