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

2012年(28)

2011年(113)

分类: 系统运维

2011-09-29 12:22:05

 

    到现在为止,我已经写了三篇关于自定义控件开发的文章,很感谢大家的支持!不知道大家对我讲的方式有什么好的建议,我尽量使自定义控件的技术让更多的程序员同胞接受。好了,开发开始了!

    本章还是延续上章的例子,开发一个CreditCardForm控件。   

    其实本章也不难,相信大家看完后,也觉得很简单!

     

    我们首先开谈谈继承Control和WebControl的区别:其实二者最大的区别就是在,如果我们开发的控件继承WebControl,那么我们的控件就支持更多的CSS样式。而继承于Control的自定义控件的样式相对就少些。

 

    其实还有一个区别就在开发的规范上。我们来简单的看看上一章的自定义控件的代码,主要来看看Render方法中的代码:大家只扫一眼就可以了。

 

  1. protected override void Render(HtmlTextWriter writer)
  2.     {
  3.         //显示table标记

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

  9.         //显示table类的标记 包含 tr ,td 还有一些textbox,下拉框等

  10.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);

  11.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  12.         writer.Write(PaymentMethodText);
  13.         writer.RenderEndTag();

  14.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  15.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod");
  16.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod");
  17.         writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
  18.         writer.RenderBeginTag(HtmlTextWriterTag.Select);

  19.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
  20.         writer.RenderBeginTag(HtmlTextWriterTag.Option);
  21.         writer.Write("Master");
  22.         writer.RenderEndTag();

  23.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
  24.         writer.RenderBeginTag(HtmlTextWriterTag.Option);
  25.         writer.Write("Visa");
  26.         writer.RenderEndTag();

  27.         writer.RenderEndTag();
  28.         writer.RenderEndTag();
  29.         writer.RenderEndTag();

  30.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  31.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  32.         writer.Write(CreditCardNoText);
  33.         writer.RenderEndTag();

  34.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  35.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
  36.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo");
  37.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo");
  38.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
  39.         writer.RenderEndTag();
  40.         writer.RenderEndTag();
  41.         writer.RenderEndTag();

  42.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  43.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  44.         writer.Write(CardholderNameText);
  45.         writer.RenderEndTag();

  46.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  47.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
  48.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName");
  49.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName");
  50.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
  51.         writer.RenderEndTag();
  52.         writer.RenderEndTag();
  53.         writer.RenderEndTag();

  54.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  55.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  56.         writer.Write(ExpirationDateText);
  57.         writer.RenderEndTag();

  58.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  59.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month");
  60.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month");
  61.         writer.RenderBeginTag(HtmlTextWriterTag.Select);
  62.         for (int month = 1; month < 13; month++)
  63.         {
  64.             writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
  65.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
  66.             writer.Write(month.ToString());
  67.             writer.RenderEndTag();
  68.         }

  69.         writer.RenderEndTag();

  70.         writer.Write(" ");

  71.         writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year");
  72.         writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year");
  73.         writer.RenderBeginTag(HtmlTextWriterTag.Select);

  74.         for (int year = 2008; year < 2015; year++)
  75.         {
  76.             writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
  77.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
  78.             writer.Write(year.ToString());
  79.             writer.RenderEndTag();
  80.         }

  81.         writer.RenderEndTag();

  82.         writer.RenderEndTag();
  83.         writer.RenderEndTag();


  84.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  85.         writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
  86.         writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
  87.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
  88.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
  89.         writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
  90.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
  91.         writer.RenderEndTag();
  92.         writer.RenderEndTag();
  93.         writer.RenderEndTag();

  94.         //table的结束标记

  95.         writer.RenderEndTag();


  96.     }

 

    相信这段代码大家都和熟悉。在这段代码的作用主要显示了一个table 以及table 之中的标记元素。

     

    如果一这个控件已经写好了,加入我们想要改改table的样式,如把border设为1,或者改变显示的颜色等,那么我们就要再次打开这段代码,修改代码。其实这就不好。为什么?

     

    其实这个方法中的代码做了四件事:

          1.显示控件最外层的包含标记,即table

          2.显示最外层标记的一些属性,如border.id等

          3.显示控件的主要内容 ,如,显示输入框,下拉框,按钮。

          4.显示控件最外层包含标记的结束标记。

 

    这样看来,这个方法的职责就有四个。大家应该还记得面向对象设计的原创:单一职责。虽然这个元素一般用在类上,但是我们更加要注意,这个原则用的宗旨:使对象只有一个变化的因素。这个原则也用在方法的设计上。

     

    就是说,我们如果想要改变最外层标记table一些属性,我们就影响了其他三个职责。同理,我们想改变其他的元素或者内容,结果可想而知。

     

    为此,继承WebControl的控件就更加的规范和灵活:

    其实,继承于WebControl的控件的显示还是在Render方法中显示,只是这个Render方法已经不由我们来写了:

    如下:

  1. protected override void Render(HtmlTextWriter writer)
  2.  {
  3.               RenderBeginTag(writer);
  4.               RenderContens(writer);
  5.               RenderEndTag(writer);

  6. }

  

    因为我们之前说,Render方法的职责有四个。所以WebControl的Render方法就把这些职责分离了。以后我们能就可以独立的改变一个因素了。大家也许奇怪:不是说有四个职责吗,但是上面只是显示了三个方法,其实在RenderBeginTag()方法中包含显示最外层标记样式的方法。

 

    所以我们只要分别重写这三个方法,一个继承WebControl的自定义的控件就成了。不要怕,方法很多,但是很简单,而且代码我们都懂,之前差不多都讲了的。
   

    我想把写成后的控件给大家看看,让大家看下我们本章的目标:

     

     

 

    大家看见没有:是不是支持的样式多了!!!

     

    好了,我们来写吧:不难哦:)

 

    首先看看我们要的写方法以及他们的功能:

 

  1. protected override void Render(HtmlTextWriter writer)
  2.          {
  3.                 //显示最外层包含标记开始的标记,即,显示

  4.               RenderBeginTag(writer);

  5.               //显示table标记内的内容,如


  6.               RenderContens(writer);

  7.                //显示最外层包含标记结束标记

  8.               RenderEndTag(writer);

  9.         }
  10.  

         现在的思路很清晰吧,首先来显示     

         很简单,只要如下就可以了。

     

    1. protected override HtmlTextWriterTag TagKey
    2. {
    3.     get
    4.     {
    5.        return HtmlTextWriterTag.Table;
    6.     }
    7. }

     

        不要奇怪,因为RenderBeginTag()方法内部实际是调用了这个属性来显示的最外层的包含标记的。

        好了,第一步就写完了啊!简答吧!

     

        下面来显示table的样式,因为我们的控件的最外层的标记是table,显示他的样式只要这样就OK了。

    1. protected override Style CreateControlStyle()
    2. {
    3.    return new TableStyle(ViewState);
    4. }

          

        又写完了一部分。。不难吧!接着看啊!

        如果你的最外层的标记是

    那么,上面的那个方法你都不用写.

        下面就写了RenderContent方法,不难,只记住这个方法的职责:显示最外层标记的内容:

        如下:

    1. protected override void RenderContents(HtmlTextWriter writer)
    2.     {
    3.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    4.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    5.         writer.Write(PaymentMethod);
    6.         writer.RenderEndTag();

    7.         writer.RenderBeginTag(HtmlTextWriterTag.Td);

    8.         writer.AddAttribute(HtmlTextWriterAttribute.Id, PaymentMethodListId);
    9.         writer.AddAttribute(HtmlTextWriterAttribute.Name, PaymentMethodListName);
    10.         writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
    11.         writer.RenderBeginTag(HtmlTextWriterTag.Select);

    12.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
    13.         writer.RenderBeginTag(HtmlTextWriterTag.Option);
    14.         writer.Write("Master");
    15.         writer.RenderEndTag();

    16.         writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
    17.         writer.RenderBeginTag(HtmlTextWriterTag.Option);
    18.         writer.Write("Visa");
    19.         writer.RenderEndTag();

    20.         writer.RenderEndTag();

    21.         writer.RenderEndTag();
    22.         writer.RenderEndTag();
    23.         //****************************************************************************

    24.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    25.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    26.         writer.Write(CreditCardNo);
    27.         writer.RenderEndTag();

    28.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    29.         writer.AddAttribute(HtmlTextWriterAttribute.Id, CreditCardNoTextId);
    30.         writer.AddAttribute(HtmlTextWriterAttribute.Name, CreditCardNoTextName);
    31.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    32.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
    33.         writer.RenderEndTag();

    34.         writer.RenderEndTag();
    35.         writer.RenderEndTag();
    36.         //**************************************************************************************


    37.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    38.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    39.         writer.Write(CardholderName);
    40.         writer.RenderEndTag();

    41.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    42.         writer.AddAttribute(HtmlTextWriterAttribute.Name, CardholderNameTextName);
    43.         writer.AddAttribute(HtmlTextWriterAttribute.Id, CardholderNameTextId);
    44.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    45.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
    46.         writer.RenderEndTag();

    47.         writer.RenderEndTag();
    48.         writer.RenderEndTag();
    49.         //***************************************************************************************


    50.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    51.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    52.         writer.Write(ExpirationDate);
    53.         writer.RenderEndTag();

    54.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    55.         writer.AddAttribute(HtmlTextWriterAttribute.Name, MonthListName);
    56.         writer.AddAttribute(HtmlTextWriterAttribute.Id, MonthListId);
    57.         writer.RenderBeginTag(HtmlTextWriterTag.Select);

    58.         for (int month = 1; month < 13; month++)
    59.         {
    60.             writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
    61.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
    62.             writer.Write(month.ToString());
    63.             writer.RenderEndTag();
    64.         }
    65.         writer.RenderEndTag();

    66.         /////////////////////////////////////


    67.         writer.Write(" ");

    68.         /////////////////////////////////////

    69.         writer.AddAttribute(HtmlTextWriterAttribute.Name, YearListName);
    70.         writer.AddAttribute(HtmlTextWriterAttribute.Id, YearListId);
    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.         //*****************************************************************************


    83.         writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    84.         writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
    85.         writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
    86.         writer.RenderBeginTag(HtmlTextWriterTag.Td);
    87.         writer.AddAttribute(HtmlTextWriterAttribute.Name, SubmitButtonName);
    88.         writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
    89.         writer.AddAttribute(HtmlTextWriterAttribute.Id, SubmitButtonId);
    90.         writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
    91.         writer.RenderBeginTag(HtmlTextWriterTag.Input);
    92.         writer.RenderEndTag();

    93.         writer.RenderEndTag();
    94.         writer.RenderEndTag();

    95.         //******************************************************************************



    96.     }

     

        这样就完了,大家看看上面的代码,很熟悉吧,是啊,只是把我们之前的代码分割开了,写在不同的地方!

     

        最后的那个RendEndTag(),你不写!因为,你override那个TagKey时,就已经调用了结束标记的方法。这样,一个继承WebControl的控件就完了!

     

         好了,写完了:完整的代码如下:    

     

    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 CreditCardForm5 : WebControl
    10.     {
    11.         #region 重写包含标记

    12.         protected override Style CreateControlStyle()
    13.         {
    14.             return new TableStyle(ViewState);
    15.         }

    16.         protected override HtmlTextWriterTag TagKey
    17.         {
    18.             get
    19.             {
    20.                 return HtmlTextWriterTag.Table;
    21.             }
    22.         }

    23.         //public virtual string BackImageUrl

    24.         //{

    25.         // get

    26.         // {

    27.         // return ((TableStyle)ControlStyle).BackImageUrl;

    28.         // }

    29.         // set

    30.         // {

    31.         // ((TableStyle)ControlStyle).BackImageUrl = value;

    32.         // }

    33.         //}



    34.         #endregion
    35.         #region ID
    36.         protected virtual string PaymentMethodListId
    37.         {
    38.             get
    39.             {
    40.                 return "PaymentMethod";
    41.             }
    42.         }

    43.         protected virtual string CreditCardNoTextId
    44.         {
    45.             get
    46.             {
    47.                 return "CreditCardNo";
    48.             }
    49.         }

    50.         protected virtual string CardholderNameTextId
    51.         {
    52.             get
    53.             {
    54.                 return "CardholderName";
    55.             }
    56.         }

    57.         protected virtual string MonthListId
    58.         {
    59.             get
    60.             {
    61.                 return "Month";
    62.             }
    63.         }

    64.         protected virtual string YearListId
    65.         {
    66.             get
    67.             {
    68.                 return "Year";
    69.             }
    70.         }

    71.         protected virtual string SubmitButtonId
    72.         {
    73.             get
    74.             {
    75.                 return "Submit";
    76.             }
    77.         }

    78.         #endregion
    79.         #region name
    80.         protected virtual string PaymentMethodListName
    81.         {
    82.             get
    83.             {
    84.                 return "PaymentMethod";
    85.             }
    86.         }

    87.         protected virtual string CreditCardNoTextName
    88.         {
    89.             get
    90.             {
    91.                 return "CreditCardNo";
    92.             }
    93.         }

    94.         protected virtual string CardholderNameTextName
    95.         {
    96.             get
    97.             {
    98.                 return "CardholderName";
    99.             }
    100.         }

    101.         protected virtual string MonthListName
    102.         {
    103.             get
    104.             {
    105.                 return "Month";
    106.             }
    107.         }

    108.         protected virtual string YearListName
    109.         {
    110.             get
    111.             {
    112.                 return "Year";
    113.             }
    114.         }

    115.         protected virtual string SubmitButtonName
    116.         {
    117.             get
    118.             {
    119.                 return "Sumit";
    120.             }
    121.         }
    122.         #endregion

    123.         #region 属性
    124.         public virtual string PaymentMethod
    125.         {
    126.             get
    127.             {
    128.                 return ViewState["PaymentMethod"] != null ? (string)ViewState["PaymentMethod"] : "支付方式";

    129.             }
    130.             set
    131.             {
    132.                 ViewState["PaymentMethod"] = value;
    133.             }
    134.         }

    135.         public virtual string CreditCardNo
    136.         {
    137.             get
    138.             {
    139.                 return ViewState["CreditCardNo"] != null ? (string)ViewState["CreditCardNo"] : "信用卡号";
    140.             }
    141.             set
    142.             {
    143.                 ViewState["CreditCardNo"] = value;
    144.             }
    145.         }

    146.         public virtual string CardholderName
    147.         {
    148.             get
    149.             {
    150.                 return ViewState["CardholderName"] != null ? (string)ViewState["CardholderName"] : "持卡人";
    151.             }
    152.             set
    153.             {
    154.                 ViewState["CreditCardName"] = value;
    155.             }
    156.         }

    157.         public virtual string ExpirationDate
    158.         {
    159.             get
    160.             {
    161.                 return ViewState["ExpirationDate"] != null ? (string)ViewState["ExpirationDate"] : "过期时间";
    162.             }
    163.             set
    164.             {
    165.                 ViewState["ExpirationDate"] = value;
    166.             }
    167.         }

    168.         public virtual string SubmitButtonText
    169.         {
    170.             get
    171.             {
    172.                 return ViewState["SubmitButtonText"] != null ? (string)ViewState["SubmitButtonText"] : "提交";
    173.             }
    174.             set
    175.             {
    176.                 ViewState["SubmitButtonText"] = value;
    177.             }

    178.         }
    179.         #endregion
    180.         protected override void RenderContents(HtmlTextWriter writer)
    181.         {
    182.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    183.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    184.             writer.Write(PaymentMethod);
    185.             writer.RenderEndTag();

    186.             writer.RenderBeginTag(HtmlTextWriterTag.Td);

    187.             writer.AddAttribute(HtmlTextWriterAttribute.Id, PaymentMethodListId);
    188.             writer.AddAttribute(HtmlTextWriterAttribute.Name, PaymentMethodListName);
    189.             writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
    190.             writer.RenderBeginTag(HtmlTextWriterTag.Select);

    191.             writer.AddAttribute(HtmlTextWriterAttribute.Value, "0");
    192.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
    193.             writer.Write("Master");
    194.             writer.RenderEndTag();

    195.             writer.AddAttribute(HtmlTextWriterAttribute.Value, "1");
    196.             writer.RenderBeginTag(HtmlTextWriterTag.Option);
    197.             writer.Write("Visa");
    198.             writer.RenderEndTag();

    199.             writer.RenderEndTag();

    200.             writer.RenderEndTag();
    201.             writer.RenderEndTag();
    202.             //****************************************************************************

    203.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    204.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    205.             writer.Write(CreditCardNo);
    206.             writer.RenderEndTag();

    207.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    208.             writer.AddAttribute(HtmlTextWriterAttribute.Id, CreditCardNoTextId);
    209.             writer.AddAttribute(HtmlTextWriterAttribute.Name, CreditCardNoTextName);
    210.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    211.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    212.             writer.RenderEndTag();

    213.             writer.RenderEndTag();
    214.             writer.RenderEndTag();
    215.             //**************************************************************************************


    216.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    217.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    218.             writer.Write(CardholderName);
    219.             writer.RenderEndTag();

    220.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    221.             writer.AddAttribute(HtmlTextWriterAttribute.Name, CardholderNameTextName);
    222.             writer.AddAttribute(HtmlTextWriterAttribute.Id, CardholderNameTextId);
    223.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
    224.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    225.             writer.RenderEndTag();

    226.             writer.RenderEndTag();
    227.             writer.RenderEndTag();
    228.             //***************************************************************************************


    229.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    230.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    231.             writer.Write(ExpirationDate);
    232.             writer.RenderEndTag();

    233.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    234.             writer.AddAttribute(HtmlTextWriterAttribute.Name, MonthListName);
    235.             writer.AddAttribute(HtmlTextWriterAttribute.Id, MonthListId);
    236.             writer.RenderBeginTag(HtmlTextWriterTag.Select);

    237.             for (int month = 1; month < 13; month++)
    238.             {
    239.                 writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString());
    240.                 writer.RenderBeginTag(HtmlTextWriterTag.Option);
    241.                 writer.Write(month.ToString());
    242.                 writer.RenderEndTag();
    243.             }
    244.             writer.RenderEndTag();

    245.             /////////////////////////////////////


    246.             writer.Write(" ");

    247.             /////////////////////////////////////

    248.             writer.AddAttribute(HtmlTextWriterAttribute.Name, YearListName);
    249.             writer.AddAttribute(HtmlTextWriterAttribute.Id, YearListId);
    250.             writer.RenderBeginTag(HtmlTextWriterTag.Select);

    251.             for (int year = 2008; year < 2015; year++)
    252.             {
    253.                 writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString());
    254.                 writer.RenderBeginTag(HtmlTextWriterTag.Option);
    255.                 writer.Write(year.ToString());
    256.                 writer.RenderEndTag();
    257.             }
    258.             writer.RenderEndTag();

    259.             writer.RenderEndTag();
    260.             writer.RenderEndTag();
    261.             //*****************************************************************************


    262.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    263.             writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
    264.             writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
    265.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
    266.             writer.AddAttribute(HtmlTextWriterAttribute.Name, SubmitButtonName);
    267.             writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
    268.             writer.AddAttribute(HtmlTextWriterAttribute.Id, SubmitButtonId);
    269.             writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
    270.             writer.RenderBeginTag(HtmlTextWriterTag.Input);
    271.             writer.RenderEndTag();

    272.             writer.RenderEndTag();
    273.             writer.RenderEndTag();

    274.             //******************************************************************************



    275.         }

    276.     }
    277. }

        希望大家继续支持啊!谢谢!

    阅读(1125) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~