Overriding __doPostBack

  • Thread starter Thread starter Mike Dunn
  • Start date Start date
M

Mike Dunn

I would like to change the name of the _doPostBack
function emmitted by the ASP.NET framework to prefix it
with an ordinal number i.e. the page should show
<script language="javascript">
<!--
function _369__doPostBack(eventTarget,
eventArgument) {
var theform =
document.frmRetailTenantGadget;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value =
eventArgument;
theform.submit();
}
// -->

</script>
The reason for this is I am working on a portal server and
the output form multiple aspx pages ends up in the same
HTML page. Multiple declarations of __doPostBack cause a
problem in the page. I have found one way to do this is to
override the page render event thus

protected override void Render
(HtmlTextWriter writer)
{
StringBuilder stringBuilder = new
StringBuilder();
StringWriter stringWriter = new
StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new
HtmlTextWriter(stringWriter);

base.Render(htmlWriter);

string html =
stringBuilder.ToString();

// Prepend postback scripts with
gadget ids
html = html.Replace
("__doPostBack", "_" + GadgetId + "__doPostBack");

writer.Write(html);
}

This works but I believe it could cause a problem with
performance as the find replace is an expensive operation.

Does anyone know a better (faster? more elegant?) way of
doing this

Regards

Mike
 
Another possible solution (not mine) but it works. I don't know how much
this slows things down, but it works well for replacing items in the output.
I use this to fix the Netscape bug in 1.1, but you could use it for other
things.

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

Dim _stringBuilder As StringBuilder = New StringBuilder()
Dim _stringWriter As StringWriter = New StringWriter(_stringBuilder)
Dim _htmlWriter As HtmlTextWriter = New HtmlTextWriter(_stringWriter)
MyBase.Render(_htmlWriter)
Dim html As String = _stringBuilder.ToString()
Dim start As Integer = html.IndexOf("document._ctl1:_ctl0;")
html = html.Replace("document._ctl1:_ctl0", "document._ctl1__ctl0")
end if
writer.Write(html)

End Sub
 
Back
Top