how can I suppress browser detection when rendering an asp.net control?
-
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.
-
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.
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
-
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
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: