简体   繁体   English

如何将 html 添加到 aspx C# 代码隐藏页面?

[英]How to add html to an aspx C# codebehind page?

I have access to a server with aspx pages.我可以访问带有 aspx 页面的服务器。 I need to add a title, parapgraphs, etc to a page.我需要在页面中添加标题、段落等。 The page currently only has the following line:该页面当前只有以下行:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>

I do not have access to the CS files, just the DLL.我无权访问 CS 文件,只有 DLL。 Anyway, when I try to add any html to the document nothing changes.无论如何,当我尝试将任何 html 添加到文档中时,没有任何变化。 I am able to change the CSS, and if I remove the "inherits" then whatever HTML I have gets displayed, but when the "inherits" is there only the default page gets displayed and none of my additions.我可以更改 CSS,如果我删除“继承”,那么无论我显示什么 HTML,但是当“继承”存在时,只有默认页面会显示,而我没有添加任何内容。

Admittedly I am new to ASP and moreover I am not trying to become a guru just to add some HTML to a page, but any advice would be great, thanks!诚然,我是 ASP 的新手,而且我并不想成为大师,只是为了在页面上添加一些 HTML,但任何建议都会很棒,谢谢!

Try putting your Page_Load embedded in the.aspx and add controls that way:尝试将您的 Page_Load 嵌入到 .aspx 中并以这种方式添加控件:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            Controls.Add(whatever);
        }
    }
</script>

<!-- Try this if the above does not work -->
<script runat="server">
        new protected void Page_Load(object sender, EventArgs e) {
        base.Page_Load(sender, e);
            if (!Page.IsPostBack) {
                Controls.Add(whatever);
            }
        }
</script>

Fundamentally, I'm afraid this is not possible.从根本上说,恐怕这是不可能的。 .NET is a single-inheritance language/framework. .NET 是一种单继承语言/框架。 So when it says Inherits="Access.Login" that means you can only have it use Access.Login OR your code-behind, but not both.因此,当它说 Inherits="Access.Login" 时,这意味着您只能使用 Access.Login 或您的代码隐藏,但不能同时使用两者。

That said, you could jump through some crazy hoops to accomplish your goal.也就是说,你可以跳过一些疯狂的箍来实现你的目标。 Like create a brand new "wrapper" page, then in the code-behind fire off an http request to the page you want.就像创建一个全新的“包装器”页面,然后在代码隐藏中向您想要的页面发出 http 请求。 Load the response, which will just be a really long string into a 3rd-party DOM parser, or if you're confident you're getting 100% valid XML back, use .NET's built-in XmlDocument or XDocument to parse the page, find your html elements, make your changes, then do a Response.Write with your modified content.将响应加载到第 3 方 DOM 解析器中,这将是一个非常长的字符串,或者如果您确信自己得到了 100% 有效的 XML,请使用 .NET 的内置 XmlDocument 或 XDocument 来解析页面,找到您的 html 元素,进行更改,然后使用修改后的内容执行 Response.Write。

And that's a real-life example of going around your elbow to get to your...这是一个真实的例子,绕过你的肘部到达你的......

I am not 100% certain this will work, but you could have a code-behind file inherit from Access.Login and use the new (override will not work if Page_Load isn't marked as virtual) keyword with Page_Load.我不能 100% 确定这会起作用,但您可以从 Access.Login 继承一个代码隐藏文件,并使用新的(如果 Page_Load 未将 Page_Load 标记为虚拟,则覆盖将不起作用)关键字与 Page_Load。 Then you could use Inherits="YourAssembly.NewLogin".然后你可以使用 Inherits="YourAssembly.NewLogin"。

The part I am not sure about is whether or not asp.net uses the page class or your subclass to call the Page_Load method.我不确定的部分是 asp.net 是否使用页面 class 或您的子类来调用 Page_Load 方法。 If page_Load was virtual, it wouldn't matter, but since it isn't the new will only be called if the page is cast into your subclass.如果 page_Load 是虚拟的,那没关系,但由于它不是 new ,只有在页面被转换到您的子类时才会调用。 It is worth a try though.不过值得一试。

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

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