开发者

How call GetWebResourceUrl() when Page is null ?

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)
     开发者_StackOverflow           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);


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(...);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜