Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1140802
  • 博文数量: 141
  • 博客积分: 3161
  • 博客等级: 中校
  • 技术积分: 3011
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-27 14:53
文章存档

2012年(28)

2011年(113)

分类: 系统运维

2011-09-29 12:22:58

    相信大家看了前几篇文章后应该觉得自定义控件很简单,无非就是把一大堆的html代码包装一下就行了。不错,前几篇写的控件确实很简单。不知道大家想过没有,难道我们每次写控件都要大费力气的把一大堆的html代码写进去吗。

     

    不知道大家试了没有:如果在write.Write()方法中写的html代码有了错误,如果把写成了,在编译的时候,编译器是不会发现错误,但是当我们把控件拖上来使用的时候,就发现控件的呈现错误。就是说,这样的错误只能到最后才能被我们发现,这样我们又得回去改代码,然后重新的编译,生成。

     

    我们开发的那个控件真的很好的,我们也也许没有考虑到浏览器版本的问题,因为不同的浏览器对html的不同版本的显示还是有区别的。看看我们的代码,我们就敢保证我们的控件在所有的浏览器上都显示的是我们想要的结果吗?

 

    基于上述原因,我们对之前的控件再来一次修改。

     

    在.NET中,我们写代码经常用到他的智能感应功能,比如我们写完后加个".",然后,后面的一些属性,方法都显示出来了 。而且在编译的时候,也提示我们一些错误信息。

 

     下面我们就来改下: 

  1. namespace CreditCardForm
  2. {
  3.      public class CreditCardForm3:CreditCardForm2
  4. {

    

    因为我们之前开发的控件已经实现了很多的特性,我们不想再意义的重新实现,所以就继承前面已经有的控件,相信大家都理解这点。

  1. writer.Write("");

     

        大家还记得这段代码吧,我们想显示一个table,而且确定他的一些属性等等。如前所说,出错的可能很大,而且兼容也有问题。怎样改?

         这样:我们用.NET里面的专门用来生成这些html标记的一些方法和属性:

    1. writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");
    2. writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");
    3. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
    4. writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");
    5. writer.RenderBeginTag(HtmlTextWriterTag.Table);

     

        这段代码和上端代码的最终的显示结果都是一样的(都生成相同的html标记)。也许大家会认为这段代码很多,难道写这么多的代码就是好的吗,还不如之前的方便。但是,想想之前所说的问题,这段代码就解决了那些问题。

     

        代码我来解释下:用AddStyleAttribute方法来添加CCS样式;AddStyleAttribute添加非CSS样式,如name,id等,用RenderBeginTag来添加开始的标记。还有一点就是这些代码都是“反的”:先定义样式,属性,最后才定义我们要写的个table标记。就是说,想给某个标记加属性,要先写属性,样式的语句,在写这个标记。     

    1. writer.RenderBeginTag(HtmlTextWriterTag.Table);

     

        看看上面的代码,显示的是个table标记,如果想显示tr?这样做:   

    1. writer.RenderBeginTag(HtmlTextWriterTag.Tr);

        大家应该明白我的意思了吧:HtmlTextWriterTag接不同的属性,就显示不同的标记。

        还有一点要特别的注意,有开始,就是结束。我们用了一个RenderBeginTag方法,就一定要有个RenderEndTag方法与之对应。

        看个例子:

    1.  writer.RenderBeginTag(HtmlTextWriterTag.Td);
    2.  writer.Write(PaymentMethodText);
    3.  writer.RenderEndTag();
         
        大家懂了吧!     

        我把代码贴上,大家看看,有问题就说!

     

    1. protected override void Render(HtmlTextWriter writer)
    2.     {

    3.         writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");

    4.         writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");

    5.         writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");

    6.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");

    7.         writer.RenderBeginTag(HtmlTextWriterTag.Table);



    8.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);



    9.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    10.         writer.Write(PaymentMethodText);

    11.         writer.RenderEndTag();



    12.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    13.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");

    14.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");

    15.         writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");

    16.         writer.RenderBeginTag(HtmlTextWriterTag.Select);



    17.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");

    18.         writer.RenderBeginTag(HtmlTextWriterTag.Option);

    19.         writer.Write("Master");

    20.         writer.RenderEndTag();



    21.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");

    22.         writer.RenderBeginTag(HtmlTextWriterTag.Option);

    23.         writer.Write("Visa");

    24.         writer.RenderEndTag();



    25.         writer.RenderEndTag();

    26.         writer.RenderEndTag();

    27.         writer.RenderEndTag();



    28.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    29.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    30.         writer.Write(CreditCardNoText);

    31.         writer.RenderEndTag();



    32.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    33.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");

    34.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");

    35.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");

    36.         writer.RenderBeginTag(HtmlTextWriterTag.Input);

    37.         writer.RenderEndTag();

    38.         writer.RenderEndTag();

    39.         writer.RenderEndTag();



    40.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    41.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    42.         writer.Write(CardholderNameText);

    43.         writer.RenderEndTag();



    44.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    45.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");

    46.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");

    47.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");

    48.         writer.RenderBeginTag(HtmlTextWriterTag.Input);

    49.         writer.RenderEndTag();

    50.         writer.RenderEndTag();

    51.         writer.RenderEndTag();



    52.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    53.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    54.         writer.Write(ExpirationDateText);

    55.         writer.RenderEndTag();



    56.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    57.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");

    58.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");

    59.         writer.RenderBeginTag(HtmlTextWriterTag.Select);

    60.         for (int month = 1; month < 13; month++)
    61.         {

    62.             writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());

    63.             writer.RenderBeginTag(HtmlTextWriterTag.Option);

    64.             writer.Write(month.ToString());

    65.             writer.RenderEndTag();

    66.         }



    67.         writer.RenderEndTag();



    68.         writer.Write(" ");



    69.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");

    70.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");

    71.         writer.RenderBeginTag(HtmlTextWriterTag.Select);



    72.         for (int year = 2008; year < 2015; year++)
    73.         {

    74.             writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());

    75.             writer.RenderBeginTag(HtmlTextWriterTag.Option);

    76.             writer.Write(year.ToString());

    77.             writer.RenderEndTag();

    78.         }



    79.         writer.RenderEndTag();



    80.         writer.RenderEndTag();

    81.         writer.RenderEndTag();





    82.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    83.         writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");

    84.         writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");

    85.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    86.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");

    87.         writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);

    88.         writer.RenderBeginTag(HtmlTextWriterTag.Input);

    89.         writer.RenderEndTag();

    90.         writer.RenderEndTag();

    91.         writer.RenderEndTag();



    92.         //table的结束标记




    93.         writer.RenderEndTag();

    94.     }

         

         下面开始说说。状态的管理,即ViewState;之前的控件还有一个问题:

         如下:

              

              

         我们在页面后面写代码:          

    1. protected void Page_Load(object sender, EventArgs e)
    2.     {
    3.         if (!this.IsPostBack)
    4.         {
    5.             CreditCardForm2_1.PaymentMethodText = " 我的支付方式";
    6.             CreditCardForm2_1.CardholderNameText = "小洋";
    7.         }
    8.     }

     

         运行页面看见:

         

     

         但是当你点击”提交“按钮,页面马上就变为了如下:

     

         

     

         变回来了!!!!?????

     

         这里就要讲讲ASP.NET的页面保存值的方式:因为现在的ASP.NET网站是基于http这种无状态协议的。大家应该都懂。

     

        在ASP.NET的我们提交的一个页面,虽然看起来是一样的,但是他们本质上已经不是同一个页面了。而是我们现在看见的页面获取了之前提交的那个页面的一些状态值。关于这点,到第四章详细讲述”页面的生命周期“中讲。

     

        所有我们就要用个ViewState来保存页面回传的值。这里我不想讲很多的理论东西,大家先看看现在的问题怎样解决:

     

    1. public override string PaymentMethodText
    2.         {
    3.             get
    4.             {
    5.                 return ViewState["PaymentMethodText"] != null ? (string)ViewState["PaymentMethodText"] : "支付方式";
    6.             }
    7.             set
    8.             {
    9.                 ViewState["PaymentMethodText"] = value;
    10.             }
    11.         }

     

         因为我们继承于CreditCardForm2,大家看看我们在CreditCardForm2中写的:    
    1. [Browsable(true)]//在属性窗口中是否可见

    2.     [Category("Appearance")]//属性的分类,如,行为,外观,大家可以在属性窗口看见这样的分类

    3.     [DefaultValue("支付方式")]
    4.     [Description("支付方式")]//这些是显示在属性窗口底下的

    5.     //virtual 方便以后二次开发

    6.     public virtual string PaymentMethodText
    7.     {
    8.         get
    9.         {
    10.             return paymentMethod;
    11.         }
    12.         set
    13.         {
    14.             paymentMethod = value;
    15.         }
    16.     }

     

       注意:Virtual。这样之后,我们的控件就完成了。完整的代码如下:希望大家支持!

       

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.Web;
    5. using System.Web.UI;
    6. using System.Web.UI.WebControls;

    7. namespace CreditCardForm
    8. {
    9.     public class CreditCardForm3 : CreditCardForm2
    10.     {
    11.         public override string PaymentMethodText
    12.         {
    13.             get
    14.             {
    15.                 return ViewState["PaymentMethodText"] != null ? (string)ViewState["PaymentMethodText"] : "支付方式";
    16.             }
    17.             set
    18.             {
    19.                 ViewState["PaymentMethodText"] = value;
    20.             }
    21.         }

    22.         public override string CreditCardNoText
    23.         {
    24.             get
    25.             {
    26.                 return ViewState["CreditCardNoText"] != null ? (string)ViewState["CreditCardNoText"] : "信用卡号";
    27.             }
    28.             set
    29.             {
    30.                 ViewState["CreditCardNoText"] = value;
    31.             }
    32.         }

    33.         public override string CardholderNameText
    34.         {
    35.             get
    36.             {
    37.                 return ViewState["CardholderNameText"] != null ? (string)ViewState["CardholderNameText"] : "持卡人";
    38.             }
    39.             set
    40.             {
    41.                 ViewState["CardholderNameText"] = value;
    42.             }
    43.         }

    44.         public override string ExpirationDateText
    45.         {
    46.             get
    47.             {
    48.                 return ViewState["ExpirationDateText"] != null ? (string)ViewState["ExpirationDateText"] : "过期时间";
    49.             }
    50.             set
    51.             {
    52.                 ViewState["ExpirationDateText"] = value;
    53.             }
    54.         }

    55.         public override string SubmitButtonText
    56.         {
    57.             get
    58.             {
    59.                 return ViewState["SubmitButtonText"] != null ? (string)ViewState["SubmitButtonText"] : "提交";
    60.             }
    61.             set
    62.             {
    63.                 ViewState["SubmitButtonText"] = value;
    64.             }
    65.         }
    66.         protected override void Render(HtmlTextWriter writer)
    67.         {
    68.             writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "287px");
    69.             writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "128px");
    70.             writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0");
    71.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "mytable");
    72.             writer.RenderBeginTag(HtmlTextWriterTag.Table);

    73.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);

    74.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    75.             writer.Write(PaymentMethodText);
    76.             writer.RenderEndTag();

    77.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    78.             writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");
    79.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");
    80.             writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
    81.             writer.RenderBeginTag(HtmlTextWriterTag.Select);

    82.             writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
    83.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
    84.             writer.Write("Master");
    85.             writer.RenderEndTag();

    86.             writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
    87.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
    88.             writer.Write("Visa");
    89.             writer.RenderEndTag();

    90.             writer.RenderEndTag();
    91.             writer.RenderEndTag();
    92.             writer.RenderEndTag();

    93.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    94.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    95.             writer.Write(CreditCardNoText);
    96.             writer.RenderEndTag();

    97.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    98.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    99.             writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");
    100.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");
    101.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    102.             writer.RenderEndTag();
    103.             writer.RenderEndTag();
    104.             writer.RenderEndTag();

    105.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    106.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    107.             writer.Write(CardholderNameText);
    108.             writer.RenderEndTag();

    109.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    110.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    111.             writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");
    112.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");
    113.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    114.             writer.RenderEndTag();
    115.             writer.RenderEndTag();
    116.             writer.RenderEndTag();

    117.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    118.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    119.             writer.Write(ExpirationDateText);
    120.             writer.RenderEndTag();

    121.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    122.             writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");
    123.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");
    124.             writer.RenderBeginTag(HtmlTextWriterTag.Select);
    125.             for (int month = 1; month < 13; month++)
    126.             {
    127.                 writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
    128.                 writer.RenderBeginTag(HtmlTextWriterTag.Option);
    129.                 writer.Write(month.ToString());
    130.                 writer.RenderEndTag();
    131.             }

    132.             writer.RenderEndTag();

    133.             writer.Write(" ");

    134.             writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");
    135.             writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");
    136.             writer.RenderBeginTag(HtmlTextWriterTag.Select);

    137.             for (int year = 2008; year < 2015; year++)
    138.             {
    139.                 writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
    140.                 writer.RenderBeginTag(HtmlTextWriterTag.Option);
    141.                 writer.Write(year.ToString());
    142.                 writer.RenderEndTag();
    143.             }

    144.             writer.RenderEndTag();

    145.             writer.RenderEndTag();
    146.             writer.RenderEndTag();


    147.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    148.             writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
    149.             writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
    150.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    151.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
    152.             writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
    153.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    154.             writer.RenderEndTag();
    155.             writer.RenderEndTag();
    156.             writer.RenderEndTag();

    157.             writer.RenderEndTag();
    158.         }
    159.     }
    160. }
    阅读(1174) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~