相信大家看了前几篇文章后应该觉得自定义控件很简单,无非就是把一大堆的html代码包装一下就行了。不错,前几篇写的控件确实很简单。不知道大家想过没有,难道我们每次写控件都要大费力气的把一大堆的html代码写进去吗。
不知道大家试了没有:如果在write.Write()方法中写的html代码有了错误,如果把写成了,在编译的时候,编译器是不会发现错误,但是当我们把控件拖上来使用的时候,就发现控件的呈现错误。就是说,这样的错误只能到最后才能被我们发现,这样我们又得回去改代码,然后重新的编译,生成。
我们开发的那个控件真的很好的,我们也也许没有考虑到浏览器版本的问题,因为不同的浏览器对html的不同版本的显示还是有区别的。看看我们的代码,我们就敢保证我们的控件在所有的浏览器上都显示的是我们想要的结果吗?
基于上述原因,我们对之前的控件再来一次修改。
在.NET中,我们写代码经常用到他的智能感应功能,比如我们写完后加个".",然后,后面的一些属性,方法都显示出来了 。而且在编译的时候,也提示我们一些错误信息。
下面我们就来改下:
- namespace CreditCardForm
- {
- public class CreditCardForm3:CreditCardForm2
- {
因为我们之前开发的控件已经实现了很多的特性,我们不想再意义的重新实现,所以就继承前面已经有的控件,相信大家都理解这点。
- writer.Write("
");
大家还记得这段代码吧,我们想显示一个table,而且确定他的一些属性等等。如前所说,出错的可能很大,而且兼容也有问题。怎样改?
这样:我们用.NET里面的专门用来生成这些html标记的一些方法和属性:
- writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");
- writer.RenderBeginTag(HtmlTextWriterTag.Table);
这段代码和上端代码的最终的显示结果都是一样的(都生成相同的html标记)。也许大家会认为这段代码很多,难道写这么多的代码就是好的吗,还不如之前的方便。但是,想想之前所说的问题,这段代码就解决了那些问题。
代码我来解释下:用AddStyleAttribute方法来添加CCS样式;AddStyleAttribute添加非CSS样式,如name,id等,用RenderBeginTag来添加开始的标记。还有一点就是这些代码都是“反的”:先定义样式,属性,最后才定义我们要写的个table标记。就是说,想给某个标记加属性,要先写属性,样式的语句,在写这个标记。
- writer.RenderBeginTag(HtmlTextWriterTag.Table);
看看上面的代码,显示的是个table标记,如果想显示tr?这样做:
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
大家应该明白我的意思了吧:HtmlTextWriterTag接不同的属性,就显示不同的标记。
还有一点要特别的注意,有开始,就是结束。我们用了一个RenderBeginTag方法,就一定要有个RenderEndTag方法与之对应。
看个例子:
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(PaymentMethodText);
- writer.RenderEndTag();
大家懂了吧!
我把代码贴上,大家看看,有问题就说!
- protected override void Render(HtmlTextWriter writer)
- {
- writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");
- writer.RenderBeginTag(HtmlTextWriterTag.Table);
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(PaymentMethodText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");
- writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write("Master");
- writer.RenderEndTag();
- writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write("Visa");
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(CreditCardNoText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(CardholderNameText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(ExpirationDateText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- for (int month = 1; month < 13; month++)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write(month.ToString());
- writer.RenderEndTag();
- }
- writer.RenderEndTag();
- writer.Write(" ");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- for (int year = 2008; year < 2015; year++)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write(year.ToString());
- writer.RenderEndTag();
- }
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
- writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
- writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- //table的结束标记
- writer.RenderEndTag();
- }
下面开始说说。状态的管理,即ViewState;之前的控件还有一个问题:
如下:
我们在页面后面写代码:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- CreditCardForm2_1.PaymentMethodText = " 我的支付方式";
- CreditCardForm2_1.CardholderNameText = "小洋";
- }
- }
运行页面看见:
但是当你点击”提交“按钮,页面马上就变为了如下:
变回来了!!!!?????
这里就要讲讲ASP.NET的页面保存值的方式:因为现在的ASP.NET网站是基于http这种无状态协议的。大家应该都懂。
在ASP.NET的我们提交的一个页面,虽然看起来是一样的,但是他们本质上已经不是同一个页面了。而是我们现在看见的页面获取了之前提交的那个页面的一些状态值。关于这点,到第四章详细讲述”页面的生命周期“中讲。
所有我们就要用个ViewState来保存页面回传的值。这里我不想讲很多的理论东西,大家先看看现在的问题怎样解决:
- public override string PaymentMethodText
- {
- get
- {
- return ViewState["PaymentMethodText"] != null ? (string)ViewState["PaymentMethodText"] : "支付方式";
- }
- set
- {
- ViewState["PaymentMethodText"] = value;
- }
- }
因为我们继承于CreditCardForm2,大家看看我们在CreditCardForm2中写的:
- [Browsable(true)]//在属性窗口中是否可见
- [Category("Appearance")]//属性的分类,如,行为,外观,大家可以在属性窗口看见这样的分类
- [DefaultValue("支付方式")]
- [Description("支付方式")]//这些是显示在属性窗口底下的
- //virtual 方便以后二次开发
- public virtual string PaymentMethodText
- {
- get
- {
- return paymentMethod;
- }
- set
- {
- paymentMethod = value;
- }
- }
注意:Virtual。这样之后,我们的控件就完成了。完整的代码如下:希望大家支持!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace CreditCardForm
- {
- public class CreditCardForm3 : CreditCardForm2
- {
- public override string PaymentMethodText
- {
- get
- {
- return ViewState["PaymentMethodText"] != null ? (string)ViewState["PaymentMethodText"] : "支付方式";
- }
- set
- {
- ViewState["PaymentMethodText"] = value;
- }
- }
- public override string CreditCardNoText
- {
- get
- {
- return ViewState["CreditCardNoText"] != null ? (string)ViewState["CreditCardNoText"] : "信用卡号";
- }
- set
- {
- ViewState["CreditCardNoText"] = value;
- }
- }
- public override string CardholderNameText
- {
- get
- {
- return ViewState["CardholderNameText"] != null ? (string)ViewState["CardholderNameText"] : "持卡人";
- }
- set
- {
- ViewState["CardholderNameText"] = value;
- }
- }
- public override string ExpirationDateText
- {
- get
- {
- return ViewState["ExpirationDateText"] != null ? (string)ViewState["ExpirationDateText"] : "过期时间";
- }
- set
- {
- ViewState["ExpirationDateText"] = value;
- }
- }
- public override string SubmitButtonText
- {
- get
- {
- return ViewState["SubmitButtonText"] != null ? (string)ViewState["SubmitButtonText"] : "提交";
- }
- set
- {
- ViewState["SubmitButtonText"] = value;
- }
- }
- protected override void Render(HtmlTextWriter writer)
- {
- writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");
- writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");
- writer.RenderBeginTag(HtmlTextWriterTag.Table);
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(PaymentMethodText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");
- writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write("Master");
- writer.RenderEndTag();
- writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write("Visa");
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(CreditCardNoText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(CardholderNameText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.Write(ExpirationDateText);
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- for (int month = 1; month < 13; month++)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write(month.ToString());
- writer.RenderEndTag();
- }
- writer.RenderEndTag();
- writer.Write(" ");
- writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");
- writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");
- writer.RenderBeginTag(HtmlTextWriterTag.Select);
- for (int year = 2008; year < 2015; year++)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
- writer.RenderBeginTag(HtmlTextWriterTag.Option);
- writer.Write(year.ToString());
- writer.RenderEndTag();
- }
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderBeginTag(HtmlTextWriterTag.Tr);
- writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
- writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
- writer.RenderBeginTag(HtmlTextWriterTag.Td);
- writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
- writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
- writer.RenderBeginTag(HtmlTextWriterTag.Input);
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- writer.RenderEndTag();
- }
- }
- }
阅读(1174) | 评论(0) | 转发(0) |