到現在為止,我已經寫了三篇關於自定義控件開發的文章,很感謝大家的支持!不知道大家對我講的 方式有什麼好的建議,我盡量使自定義控件的技術讓更多的程序員同胞接受。好了,開發開始了!
本章還是延續上章的例子,開發一個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 10 //顯示table類的標記 包含 tr ,td 還有一些textbox,下拉框等 11 writer.RenderBeginTag(HtmlTextWriterTag.Tr); 12 13 writer.RenderBeginTag(HtmlTextWriterTag.Td); 14 writer.Write(PaymentMethodText); 15 writer.RenderEndTag(); 16 17 writer.RenderBeginTag(HtmlTextWriterTag.Td); 18 writer.AddAttribute(HtmlTextWriterAttribute.Name, "PaymentMethod"); 19 writer.AddAttribute(HtmlTextWriterAttribute.Id, "PaymentMethod"); 20 writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%"); 21 writer.RenderBeginTag(HtmlTextWriterTag.Select); 22 23 writer.AddAttribute(HtmlTextWriterAttribute.Value, "0"); 24 writer.RenderBeginTag(HtmlTextWriterTag.Option); 25 writer.Write("Master"); 26 writer.RenderEndTag(); 27 28 writer.AddAttribute(HtmlTextWriterAttribute.Value, "1"); 29 writer.RenderBeginTag(HtmlTextWriterTag.Option); 30 writer.Write("Visa"); 31 writer.RenderEndTag(); 32 33 writer.RenderEndTag(); 34 writer.RenderEndTag(); 35 writer.RenderEndTag(); 36 37 writer.RenderBeginTag(HtmlTextWriterTag.Tr); 38 writer.RenderBeginTag(HtmlTextWriterTag.Td); 39 writer.Write(CreditCardNoText); 40 writer.RenderEndTag(); 41 42 writer.RenderBeginTag(HtmlTextWriterTag.Td); 43 writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); 44 writer.AddAttribute(HtmlTextWriterAttribute.Name, "CreditCardNo"); 45 writer.AddAttribute(HtmlTextWriterAttribute.Id, "CreditCardNo"); 46 writer.RenderBeginTag(HtmlTextWriterTag.Input); 47 writer.RenderEndTag(); 48 writer.RenderEndTag(); 49 writer.RenderEndTag(); 50 51 writer.RenderBeginTag(HtmlTextWriterTag.Tr); 52 writer.RenderBeginTag(HtmlTextWriterTag.Td); 53 writer.Write(CardholderNameText); 54 writer.RenderEndTag(); 55 56 writer.RenderBeginTag(HtmlTextWriterTag.Td); 57 writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); 58 writer.AddAttribute(HtmlTextWriterAttribute.Name, "CardholderName"); 59 writer.AddAttribute(HtmlTextWriterAttribute.Id, "CardholderName"); 60 writer.RenderBeginTag(HtmlTextWriterTag.Input); 61 writer.RenderEndTag(); 62 writer.RenderEndTag(); 63 writer.RenderEndTag(); 64 65 writer.RenderBeginTag(HtmlTextWriterTag.Tr); 66 writer.RenderBeginTag(HtmlTextWriterTag.Td); 67 writer.Write(ExpirationDateText); 68 writer.RenderEndTag(); 69 70 writer.RenderBeginTag(HtmlTextWriterTag.Td); 71 writer.AddAttribute(HtmlTextWriterAttribute.Name, "Month"); 72 writer.AddAttribute(HtmlTextWriterAttribute.Id, "Month"); 73 writer.RenderBeginTag(HtmlTextWriterTag.Select); 74 for (int month = 1; month < 13; month++) 75 { 76 writer.AddAttribute(HtmlTextWriterAttribute.Value, month.ToString()); 77 writer.RenderBeginTag(HtmlTextWriterTag.Option); 78 writer.Write(month.ToString()); 79 writer.RenderEndTag(); 80 } 81 82 writer.RenderEndTag(); 83 84 writer.Write(" "); 85 86 writer.AddAttribute(HtmlTextWriterAttribute.Name, "Year"); 87 writer.AddAttribute(HtmlTextWriterAttribute.Id, "Year"); 88 writer.RenderBeginTag(HtmlTextWriterTag.Select); 89 90 for (int year =2008; year < 2015; year++) 91 { 92 writer.AddAttribute(HtmlTextWriterAttribute.Value, year.ToString()); 93 writer.RenderBeginTag(HtmlTextWriterTag.Option); 94 writer.Write(year.ToString()); 95 writer.RenderEndTag(); 96 } 97 98 writer.RenderEndTag(); 99 100 writer.RenderEndTag(); 101 writer.RenderEndTag(); 102 103 104 writer.RenderBeginTag(HtmlTextWriterTag.Tr); 105 writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2"); 106 writer.AddAttribute(HtmlTextWriterAttribute.Align, "center"); 107 writer.RenderBeginTag(HtmlTextWriterTag.Td); 108 writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit"); 109 writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText); 110 writer.RenderBeginTag(HtmlTextWriterTag.Input); 111 writer.RenderEndTag(); 112 writer.RenderEndTag(); 113 writer.RenderEndTag(); 114 115 //table的結束標記 116 writer.RenderEndTag(); 117 118 119 120 121 }