WebControl.Net属性映射到HTML上的属性 -[ .net WEB_Technic ]

来源: BlogBus 原始链接: http://craftsman.blogbus.com:80/s581/index.html 存档链接: https://web.archive.org/web/20060219095934id_/http://craftsman.blogbus.com:80/s581/index.html


程序工匠 我的职业随笔,一个IT技术人员的技术随笔。 / 首页 / 分页: 2005-11-18 14:08:11 WebControl.Net属性映射到HTML上的属性 -[ .net WEB_Technic ] 好久没有在我的技术Blog上Post了,先说些闲话。 昨天CSDN的 ghj 在blog上提到 一个问题 。关于Asp.net的WebControl的属性。他发现TextBox在IE浏览器上能够正常显示,但是到了Firefox上有些外观效果就不出来了。 比如如下的标签: <asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" Width="432px" Height="256px"></asp:TextBox> 在IE6.0(4.0之上都可以)下,被转成如下代码:

而在Firefox下,则没有了style,变成:

事实上,不光是TextBox的问题,比如Button、Table之类的Webcontrol也有一样的问题,他们都继承了WebControl这个类,WebControl的会根据浏览器的不同,选择某些属性以不同的方式呈现,或者某些属性根本无法呈现,WebControl根据一张支持html4.0浏览器表,将属性用html4.0 样式特性来呈现,而它不知道的浏览器选择用符合HTML3.2的特性来呈现。 Width,Height这种WebControl的属性,它会映射为html的style中的height和width上,而html3.2是不支持使用style,也没有对应的高和宽的定义,因此就无法实现映射了。在某些WebControl中ForeColor这种属性,如果映射为html4.0,他将是style的color属性,如果映射为html3.2,他将变成映射到上的color属性。 这并非MS故意为之,也不是说凡是ie浏览器webcontrol就将属性映射到html4.0上,如果你用ie3.0,那么它的效果也和firefox一样。 自己写一个socket程序,把下面这段http报文发送给服务器看看: GET /test.aspx HTTP/1.1 Host: localhost User-Agent:Mozilla/4.0 (compatible; MSIE 3.0; Windows NT 5.0) Connection: Keep-Alive Accept-Encoding:gzip,deflate 结果和Firefox、Flock之类的浏览器一样的,如果你把MSIE 3.0改成4.0或者之上的版本他就能返回你想要的代码了。 其实只要扩大支持html4.0的浏览器表就可以解决这个问题了,ghj说asp.net2.0已经解决了这个问题,我还没有研究过asp.net 2.0,不知道它的webcontrol怎么解决的了。http1.1报文并没有关于浏览器支持的html格式的声明,大概只能加入了一个Firefox浏览器标识判断的缘故的吧。 对于这个问题,ghj提供的解决方案是使用,动态添加WebControl的Attributes属性,比如: TextBox1.Attributes.Add("style","height:256px;width:432px") 其实也可以直接把style写到webcontrol的标签上也能够实现,比如: <asp:TextBox id="TextBox1" style="width:100px;height:100px" runat="server" TextMode="MultiLine"></asp:TextBox> WebControl不会把你自己加上去的属性给去掉的,就算你加上一个莫名其妙的属性,比如:test="test"之类的。 我一点也不喜欢WebControl默认给你的外观属性,还不如通过style属性来直接控制html元素的外观。对UI的定制控制,外置的css可控性更强。 发现,已经有半年没有更新这个技术Blog了。这半年几乎三个月是辞职后呆在家休息、反思。另外些日子忙个破项目,找工作。其他的blog还能不时保持更新。 hob.mount 发表于 14:08:11 | 阅读全文 | 评论(0) | 引用Trackback(0) | 编辑 2005-05-18 12:08:41 关于DotNet程序通过ProxyServer访问Web页面的异常 -[ .net Exception 网络编程 ] 前两天我写了一个Dot Net的应用程序,发现有这么一个问题。 在公司局域网内使用 System.Net.HttpWebRequest 请求一个网页,需要通过ISAServer代理出去,使用 System.Net.WebProxy 来设置代理服务器。如果请求的URL是一个HTTP协议的,那么可以获得网页内容,但是如果请求的URL是HTTPS协议的,那么程序发生错误,抛出一个WebExcption异常。程序代码如下: public class Form1 : System.Windows.Forms.Form { #region System.Window.Forms Componet private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox txtPassword; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtDomain; private System.Windows.Forms.TextBox txtUrl; private System.Windows.Forms.TextBox txtRequestHtml; private System.Windows.Forms.Button button2; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label lblProxyServer; #endregion /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null ; private Uri proxyUri = new Uri( "http://172.16.1.4:2000" ); private string [] proxyPassList = new string []{ "https://172.16.222.49" }; private WebProxy proxyServer = new WebProxy(); public Form1() { // // Windows 窗体设计器支持所必需的 // this .proxyServer.BypassList = proxyPassList; this .proxyServer.Address = this .proxyUri; this .proxyServer.BypassProxyOnLocal = true ; InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) ... #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() ... #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run( new Form1()); } private void button1_Click( object sender, System.EventArgs e) { NetworkCredential myCred = new NetworkCredential( this .txtUserName.Text, this .txtPassword.Text, this .txtDomain.Text); this .proxyServer.Credentials = myCred; this .lblProxyServer.Text = "ProxyServer = " + this .proxyUri.ToString(); System.Text.StringBuilder htmlStrBd = new System.Text.StringBuilder();; try { Uri myUri = new Uri( this .txtUrl.Text); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(myUri); request.Proxy = this .proxyServer; using (WebResponse response = request.GetResponse()){ System.IO.Stream resStrm = response.GetResponseStream(); System.IO.StreamReader htmlStream = new System.IO.StreamReader(resStrm,System.Text.Encoding.GetEncoding( "gb2312" )); int readNumBytes = 0; do { char [] htmlBlock = new Char[1024]; readNumBytes = htmlStream.Read(htmlBlock,0,1024); htmlStrBd.Append(htmlBlock,0,readNumBytes); } while (readNumBytes > 0); htmlStream.Close(); response.Close(); } this .txtRequestHtml.Text = htmlStrBd.ToString(); } catch (System.Exception ex){ System.Diagnostics.Trace.WriteLine(e.ToString()); throw new Exception(ex.ToString()); } } private void button2_Click( object sender, System.EventArgs e) { Application.Exit(); } } 在公司局域网内,使用浏览器是可以访问HTTPS到网页内容的,ISAServer设置是任何没有问题的,我以为是 System.Net.WebProxy 设 置的问题,而一个同事说,可能是数字认证失败的结果。一般使用浏览器访问一个HTTPS站点时,客户端会获得一个数字证书,如果该证书是不受信任的,那么 弹出对话框给你一个安全提示,选择是否继续,浏览器中你可以通过人工的选择是否访问网页信息。.Net程序通过一些证书策略来验证网页, System.Net.ServicePointManager 的 CertificatePolicy 属性负责证书的信任策略的,默认情况下,只允许有效的证书,如果有数字证书过期或者地址不匹配之类的问题,那么HttpWeb请求就发生一个“TrustFailure”的异常。下面这个Blog的Post谈到了这个问题: http://weblogs.asp.net/jan/posts/41154.aspx ...... hob.mount 发表于 12:08:41 | 阅读全文 | 评论(0) | 引用Trackback(0) | 编辑 分页: 最后更新 最新评论 存档 我的链接