Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how can I suppress browser detection when rendering an asp.net control?

how can I suppress browser detection when rendering an asp.net control?

Scheduled Pinned Locked Moved ASP.NET
helpquestioncsharphtmlcss
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spazzman
    wrote on last edited by
    #1

    I've written a custom server control. In the render method I use the HtmlTextWriter object to render all my HTML. Fine. It works great in Internet Explorer, but when I view the control in Firefox, most of the CSS stuff is removed by the HTML rendering process. If I save the IE output as a static HTML page and view that in Firefox, it works fine, so obviously the CSS stuff I wrote works for both IE and Firefox. The Asp.net rendering process just doesn't want to output all the style stuff when it encounters Firefox. After searching the web, I found a "solution" to this problem : add this the web.config ... tagwriter=System.Web.UI.HtmlTextWriter Asp.net then ignores the browser and just dumps out the HTML. That works fine for both IE and Firefox. My problem is, I dont want to have to edit the web.config file just to make a control render properly. It seems like overkill. I know that if I use Microsoft's own controls, like buttons or textfields, they work just fine in Firefox, no web.config editting required. I would like to have the same for my own server controls. Is there anyway I can force this ignoring of browser, from _within_ my server control? It has to be something I set in my code, not something I set in the web.config or machine.config. Any help would be greatly appreciated.

    M 1 Reply Last reply
    0
    • S spazzman

      I've written a custom server control. In the render method I use the HtmlTextWriter object to render all my HTML. Fine. It works great in Internet Explorer, but when I view the control in Firefox, most of the CSS stuff is removed by the HTML rendering process. If I save the IE output as a static HTML page and view that in Firefox, it works fine, so obviously the CSS stuff I wrote works for both IE and Firefox. The Asp.net rendering process just doesn't want to output all the style stuff when it encounters Firefox. After searching the web, I found a "solution" to this problem : add this the web.config ... tagwriter=System.Web.UI.HtmlTextWriter Asp.net then ignores the browser and just dumps out the HTML. That works fine for both IE and Firefox. My problem is, I dont want to have to edit the web.config file just to make a control render properly. It seems like overkill. I know that if I use Microsoft's own controls, like buttons or textfields, they work just fine in Firefox, no web.config editting required. I would like to have the same for my own server controls. Is there anyway I can force this ignoring of browser, from _within_ my server control? It has to be something I set in my code, not something I set in the web.config or machine.config. Any help would be greatly appreciated.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, As you know that ASP.NET engine detects the browser to determine which writer is used to render the control contents: HtmlTextWriter or Html32TextWriter. You can get information about the writer with the help from the TagWriter property of the Browser object. Basically, the HtmlTextWriter is set for IE 4 and later, and Html32TextWriter for non-Microsoft browsers such as Firefox .... This setting can be configured as you are doing now in order to set the HtmlTextWriter for all browsers. Here, if you don't want to place the setting in the config file, you will then have to replace the default writer with the HtmlTextWriter in code, and Reflection is a good option. The default writer is basically saved in a private member _tagwriter of the HttpBrowserCapabilities, with Reflection you can easily reset this field. In addition, you may also need to do a checking on the browser information to decide if you need to replace or not. The sample code below demonstrates how to replace the writer:

      Type writerType = typeof(System.Web.UI.HtmlTextWriter);
      Type browserType = typeof(System.Web.HttpBrowserCapabilities);
      browserType.InvokeMember("_tagwriter", BindingFlags.Instance | BindingFlags.NonPublic |
      BindingFlags.SetField, null, Request.Browser, new object[]{writerType});

      Another approach comes to mind is that in the Render methods you normally use the passed in parameter HtmlTextWriter to render the control, and depending on the browser it can be HtmlTextWriter or Html32TextWriter. Now, you simply create a new HtmlTextWriter object which is used to render control, then you can assign the output to the method parameter. The sample code is something like this:

      protected override void Render(HtmlTextWriter writer)
      {
      StringWriter tw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(tw);

      //Now you can use the hw object to render the control.
      ...
      
      //Finally, write the output to the default writer.
      writer.Write(tw.ToString());
      

      }

      However, in my opinion the easier way might be to use the browserCaps element in the Web.config file. For more information, you can take a look at some documents below: http://www.codeproject.com/aspnet/browsercaps.asp[^] http://aspnet.4guysfromrolla.com/a

      S 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, As you know that ASP.NET engine detects the browser to determine which writer is used to render the control contents: HtmlTextWriter or Html32TextWriter. You can get information about the writer with the help from the TagWriter property of the Browser object. Basically, the HtmlTextWriter is set for IE 4 and later, and Html32TextWriter for non-Microsoft browsers such as Firefox .... This setting can be configured as you are doing now in order to set the HtmlTextWriter for all browsers. Here, if you don't want to place the setting in the config file, you will then have to replace the default writer with the HtmlTextWriter in code, and Reflection is a good option. The default writer is basically saved in a private member _tagwriter of the HttpBrowserCapabilities, with Reflection you can easily reset this field. In addition, you may also need to do a checking on the browser information to decide if you need to replace or not. The sample code below demonstrates how to replace the writer:

        Type writerType = typeof(System.Web.UI.HtmlTextWriter);
        Type browserType = typeof(System.Web.HttpBrowserCapabilities);
        browserType.InvokeMember("_tagwriter", BindingFlags.Instance | BindingFlags.NonPublic |
        BindingFlags.SetField, null, Request.Browser, new object[]{writerType});

        Another approach comes to mind is that in the Render methods you normally use the passed in parameter HtmlTextWriter to render the control, and depending on the browser it can be HtmlTextWriter or Html32TextWriter. Now, you simply create a new HtmlTextWriter object which is used to render control, then you can assign the output to the method parameter. The sample code is something like this:

        protected override void Render(HtmlTextWriter writer)
        {
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        //Now you can use the hw object to render the control.
        ...
        
        //Finally, write the output to the default writer.
        writer.Write(tw.ToString());
        

        }

        However, in my opinion the easier way might be to use the browserCaps element in the Web.config file. For more information, you can take a look at some documents below: http://www.codeproject.com/aspnet/browsercaps.asp[^] http://aspnet.4guysfromrolla.com/a

        S Offline
        S Offline
        spazzman
        wrote on last edited by
        #3

        hey, thanks a lot! some good info in there. I like the fact that you are proposing several solutions. Gives me something to choose from. I dont want to use the Web.config solution because I want my controls to be proper drag-and-drop, and having to modify the web.config kinda breaks that. Besides, if the default asp.net controls work in all browsers, why can't mine? Thanks again!:cool:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups