gpt4 book ai didi

asp.net - HtmlGenericControl 对象引用未设置为对象的实例

转载 作者:行者123 更新时间:2023-11-28 11:03:59 28 4
gpt4 key购买 nike

我正在使用一个非常简单的 HtmlGenericControl 代码,它只是在我的代码后面动态添加 CSS它给了我未设置为对象实例的对象引用,如您所见,它显然不为空

HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "header{"+ imagePath +";}";
Page.Header.Controls.Add(style);

最佳答案

它在哪一行抛出异常?哪个对象是违规对象?在您的代码中添加断点并告诉我们。会不会是imagePath没有设置正确?

更新:

我刚刚创建了一个全新的 C# WebForms 元素,并将以下内容添加到 Default.aspx.cs 的 Page_Load 方法中,然后检查了源代码,它完全符合您的预期:

var imagePath = "contentHere";
HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "header{" + imagePath + ";}";
Page.Header.Controls.Add(style);

这被添加到我的 HTML 页面的 head 标签的末尾:

<style type="text/css">header{contentHere;}</style>

为了确认,我在页面上有以下 using 语句:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

关于asp.net - HtmlGenericControl 对象引用未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22326632/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com