当 Page 为 null 时如何调用 GetWebResourceUrl() ?

发布于 2024-11-27 11:11:23 字数 2396 浏览 1 评论 0原文

我构建了自定义 ASP.NET 控件,当我手动(拖放)或通过代码将其添加到标记中的控件时,它工作正常。
自定义控件 MsgBox 嵌入了 JavaScript、CSS 和图像等资源,当我尝试在类中渲染控件以返回其 HTML 代码时出现问题,Page 实例为 null 并且“GetWebResourceUrl”需要它:

Page.ClientScript.GetWebResourceUrl(.....)  

是否存在有什么方法可以获取resourceurl吗?这是我的渲染代码:

protected override void RenderContents(HtmlTextWriter writer)
{
    using (PlaceHolder plh = new PlaceHolder())
    {
        if (Page != null)
        {
            if (DesignMode || Page.Header == null)
                RegisterCSSInclude(plh);
        }  
        HtmlGenericControl container = new HtmlGenericControl("div");
        container.EnableViewState = false;
        container.InnerHtml = "Control html code";  
        plh.Controls.Add(container);
        plh.RenderControl(writer);
    }
}

RegisterCSSInclude 是注册我的 css 文件的方法:

private void RegisterCSSInclude(Control target)
{
    // CSS                   
    bool linkIncluded = false;
    foreach (Control c in target.Controls)
    {
        if (c.ID == "MsgBxStyle")
        {
            linkIncluded = true;
        }
    }
    if (!linkIncluded)
    {
        HtmlGenericControl globalCsslink = new HtmlGenericControl("link");
        globalCsslink.ID = "MsgBxGStyle";
        globalCsslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles.WeDevMsgBox.css"));
        globalCsslink.Attributes.Add("type", "text/css");
        globalCsslink.Attributes.Add("rel", "stylesheet");
        globalCsslink.EnableViewState = false;
        target.Controls.Add(globalCsslink);  
        HtmlGenericControl csslink = new HtmlGenericControl("link");
        csslink.ID = "MsgBxStyle";
        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles." + Style.ToString().ToLower() + ".css"));
        csslink.Attributes.Add("type", "text/css");
        csslink.Attributes.Add("rel", "stylesheet");
        csslink.EnableViewState = false;
        target.Controls.Add(csslink);
    }
}  

更新:
PS:我尝试在通用处理程序(ashx)中使用控件,其中我调用 ShowMsgBox 方法,该方法是类中的方法,而不是页面或用户控件中的方法。
ShowMsgBox 方法应该创建 MsgBox 控件的实例并呈现它,然后将 html 代码返回到 ashx 类:

var htmlCode = MyClass.ShowMsgBox("myMsg");
context.Response.write(htmlCode);

I built custom ASP.NET control and it's working fine when I add it manually (drag and drop) or by code to controls in markup.
The custom control, MsgBox, had resources like JavaScript, CSS and images embedded in and the problem appeared when I tried to Render the control in class to return its HTML code, the Page instance is null and the "GetWebResourceUrl" needs it:

Page.ClientScript.GetWebResourceUrl(.....)  

is there any way to get the resourceurl ? Here is my render code:

protected override void RenderContents(HtmlTextWriter writer)
{
    using (PlaceHolder plh = new PlaceHolder())
    {
        if (Page != null)
        {
            if (DesignMode || Page.Header == null)
                RegisterCSSInclude(plh);
        }  
        HtmlGenericControl container = new HtmlGenericControl("div");
        container.EnableViewState = false;
        container.InnerHtml = "Control html code";  
        plh.Controls.Add(container);
        plh.RenderControl(writer);
    }
}

RegisterCSSInclude is method to register my css files:

private void RegisterCSSInclude(Control target)
{
    // CSS                   
    bool linkIncluded = false;
    foreach (Control c in target.Controls)
    {
        if (c.ID == "MsgBxStyle")
        {
            linkIncluded = true;
        }
    }
    if (!linkIncluded)
    {
        HtmlGenericControl globalCsslink = new HtmlGenericControl("link");
        globalCsslink.ID = "MsgBxGStyle";
        globalCsslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles.WeDevMsgBox.css"));
        globalCsslink.Attributes.Add("type", "text/css");
        globalCsslink.Attributes.Add("rel", "stylesheet");
        globalCsslink.EnableViewState = false;
        target.Controls.Add(globalCsslink);  
        HtmlGenericControl csslink = new HtmlGenericControl("link");
        csslink.ID = "MsgBxStyle";
        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles." + Style.ToString().ToLower() + ".css"));
        csslink.Attributes.Add("type", "text/css");
        csslink.Attributes.Add("rel", "stylesheet");
        csslink.EnableViewState = false;
        target.Controls.Add(csslink);
    }
}  

Update:
PS: I'm tring to use control in generic handler (ashx) where I call ShowMsgBox method which is a method in a class and not in a page or user control.
ShowMsgBox method should create an instance of MsgBox control and render it then return the html code to ashx class :

var htmlCode = MyClass.ShowMsgBox("myMsg");
context.Response.write(htmlCode);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-04 11:11:23

我构建了一个自定义 ASP.NET 控件...
我尝试在通用处理程序(ashx)中使用控件...而不是在页面或用户控件中。

Page一个处理程序。您想要使用 Page 类提供的便利,但不想从 Page 继承。 Page 的优点(例如 ClientScript)期望有一个 Page 从中获取各种信息。

您可以通过设置自定义 ControlPage 属性来向控件提供虚拟 Page 对象:

this.Page = new Page();

...然后您需要设置ClientScriptManager.GetWebResourceUrl() 需要的各种属性(假设它们是 public):

this.Page.Foo = "bar"; 

那么您可以调用:

this.Page.ClientScript.GetWebResourceUrl(...);

I built a custom ASP.NET control ...
I'm tring to use control in generic handler (ashx) ... not in a page or user control.

A Page is a handler. You want to use a convenience provided by the Page class, but you don't want to inherit from Page. The niceties of Page, such as ClientScript, expect a Page from which to get various information.

You can provide a dummy Page object to your control by setting the Page property of your custom Control:

this.Page = new Page();

...then you will need to set various properties (assuming they are public) which are expected by ClientScriptManager.GetWebResourceUrl():

this.Page.Foo = "bar"; 

then you can call:

this.Page.ClientScript.GetWebResourceUrl(...);
Saygoodbye 2024-12-04 11:11:23

如果这是从 WebControl 页面继承的特定类,则不应为 null。如果您将其呈现为层次结构的一部分的另一个类,则可以添加 Page 类型的参数并将当前页面的引用传递给它。

If this is the specific class that inherits from WebControl page shouldn't be null. If its another class that you are rendering as part of a hierarchy you can add a parameter of type Page and pass the reference of the current page to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文