简体   繁体   English

我需要帮助使用 C# 将代码插入到 ASP.NET 页面的末尾

[英]I need help inserting code to the end of the ASP.NET page with C#

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 TIA

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.并让 Asp.Net 处理放置它的位置。

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)通常,我们不会尝试将脚本附加到“在</body>结束标记之后”的事件侦听器以确保它在页面加载后执行,而是这样做:(使用 jquery,仅作为示例) )

$(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.但是好吧,考虑到你有这个特定的任务(比如从早期的项目中呈现准确的 HTML 输出),这里有一个 BAD BAD BAD hack 应该可以解决问题。

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() .重要的类Control (所有 Web 控件都从该类扩展)使我们可以通过使用SetRenderMethodDelegate()分配委托函数来完全覆盖控件的呈现。

What we do is, we first set it for theBody (our actual body) to have our delegate method invoked,我们所做的是,我们首先为theBody (我们的实际主体)设置它来调用我们的委托方法,

And when we are actually invoked, we hack into Control and erase it and Render the <body> as usual and then render our script.当我们真正被调用时,我们侵入 Control 并擦除它并像往常一样渲染<body> ,然后渲染我们的脚本。

Here is the implementation:这是实现:

First, give your <body> and id and set runat="Server" to access it from the code behind:首先,给你的<body>和 id 并设置 runat="Server" 从后面的代码访问它:

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:最后,覆盖 OnPreRenderComplete 并设置我们的委托:

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.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM