It is a very interesting requirement.
Normally, we add a script to a page by using:
RegisterClientScriptBlock("myScript", MY_SCRIPT_CODE);
And let Asp.Net handle where to put it.
And normally, we don't try to put a script to attach to an event listener "after the </body> closing tag" to make sure it executes after the page is loaded, but do: (using jquery, just as an example)
$(document).ready(function() {
$("#rq1").on('click', function() { $("#myBtn").trigger('click'); });
}
But ok, thinking that you have this specific assignment (like rendering the exact HTML output as-is from an earlier project), here is a BAD BAD BAD hack which should do the trick.
the significant class Control (from which all web controls extend) gives us the possibility to completely override the rendering of the control by assigning a delegate function using SetRenderMethodDelegate().
What we do is, we first set it for theBody (our actual body) to have our delegate method invoked,
And when we are actually invoked, we hack into Control and erase it and Render the <body>as usual and then render our script.
Here is the implementation:
First, give your <body> and id and set runat="Server" to access it from the code behind:
ASPX:
<body runat="server" id="theBody">
Then, implement this in your code behind:
ASPX.CS:
private void RenderMyScript(HtmlTextWriter output, Control container)
{
object controlRareFields = typeof(Control)
.GetProperty("RareFieldsEnsured", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(theBody, null);
controlRareFields.GetType().GetField("RenderMethod").SetValue(controlRareFields, null);
theBody.RenderControl(output);
output.Write(MY_SCRIPT_CODE);
}
And finally, override OnPreRenderComplete and set our delegate:
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
theBody.SetRenderMethodDelegate(RenderMyScript);
}
And that's all.
Here are the last few lines my page output:
<!-- End Browser Link -->
</body>
<script>
document.getElementById('rq1').addEventListener('click', function () {;
document.getElementById('myBtn').click();
});
</script>
</body>
</html>
Good luck.