Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:43:17+00:00 2026-05-24T04:43:17+00:00

I built custom ASP.NET control and it’s working fine when I add it manually

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:43:18+00:00Added an answer on May 24, 2026 at 4:43 am

    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(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an already built ASP.Net custom date control, it seems pretty simple,
I use a custom-built asp.net control that renders to a DIV and has height='0'
I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I've got a custom ASP.Net web user control I've built; done this enough times
I have a custom control that mimics to some extent the built in ASP.Net
I've built a custom login system for my asp.net mvc 1.0 web application as
I have an ASP control custom built and I need to pass a value
there is a sample code for creating a custom textbox control with built in
I'm trying to build a custom control in ASP.NET MVC. The thing that I
I am trying to write a template custom control in C#, ASP.NET that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.