0

I've been trying to make the following to work with no luck

string MY_SCRIPT_CODE = "<script>";
MY_SCRIPT_CODE += " document.getElementById('rq1').addEventListener('click', function () {";
MY_SCRIPT_CODE += "     document.getElementById('myBtn').click();";
MY_SCRIPT_CODE += "  });";
MY_SCRIPT_CODE += "</script>"; 
System.Web.UI.HtmlControls.HtmlGenericControl bodyTag = new System.Web.UI.HtmlControls.HtmlGenericControl();
bodyTag = (System.Web.UI.HtmlControls.HtmlGenericControl)(Page.FindControl("</body>"));
bodyTag.Controls.Add(new LiteralControl(MY_SCRIPT_CODE));

<!-- Final Result should placed after the HTML Tag </body> --> 
<!-- and before the HTML Tag </html> after insertion -->
</body>
    <!-- Insertion from the C# Code  --->
    <script>
          document.getElementById("rq1").addEventListener('click', function () {
              document.getElementById("myBtn").click();
          });
    </script>
</html>

Is the above mentioned possible?

TIA

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jose Saiz
  • 7
  • 4
  • Well you're adding the script in a very strange way and obviously you're adding it to `bodyTag`, so I don't know why you think it would appear after the body tag. Any reason why you aren't using [RegisterStartupScript or RegisterClientScriptBlock](https://stackoverflow.com/questions/666519/difference-between-registerstartupscript-and-registerclientscriptblock)? That is how you'd normally do it with web forms. – John Wu Apr 01 '20 at 23:37
  • Dear Jose, can you please interact in any way? Did my answer solve your problem? – Oguz Ozgul Apr 03 '20 at 22:18

1 Answers1

0

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.

Oguz Ozgul
  • 6,809
  • 1
  • 14
  • 26
  • Thanks Oguz for your reply, I like it, I think that should work by placing the body tag to runat as server side then rendering the script. Thank you much for your reply and help. – Jose Saiz May 25 '20 at 14:29