1.AdRotator 随机展示广告图片,并链接到指定地址.随机内容由XML文件指定.代码如下: <asp:AdRotator id="ar1" AdvertisementFile="Ads.xml" BorderWidth="1" runat=server /> <Advertisements> <Ad> <ImageUrl>images/banner1.gif</ImageUrl> <NavigateUrl>http://www.microsoft.com</NavigateUrl> <AlternateText>Microsoft.com</AlternateText> <Keyword>Computers</Keyword> <Impressions>80</Impressions> </Ad> </Advertisements>
2.BulletedList 创建带标识符号列表项.也可呈现超级链接列表. <asp:BulletedList ID=BulletedList1 BulletStyle="Circle" runat=server> <asp:ListItem>Item #1</asp:ListItem> <asp:ListItem>Item #2</asp:ListItem> <asp:ListItem>Item #3</asp:ListItem> <asp:ListItem>Item #4</asp:ListItem> </asp:BulletedList> <asp:BulletedList DisplayMode = HyperLink runat=server> <asp:ListItem Value="http://support.Microsoft.com">Support</asp:ListItem> <asp:ListItem Value="http://www.asp.net">ASP.NET</asp:ListItem> <asp:ListItem Value="http://msdn.microsoft.com">MSDN</asp:ListItem> </asp:BulletedList>
3.Button 按钮传递一个页面回服务器.也可用于绑定型控件激活某个事件.也可定义按钮各种效果. <asp:Button id=Button1 Text="Click Me" onclick="Button1_Click" runat="server" /> <asp:Repeater id=repeater1 onitemcommand="Repeater1_ItemCommand" runat="server"> <ItemTemplate> <asp:Button id=btnBuy Text="Buy" CommandName="buy" CommandArgument='<%# DataBinder.eval_r(Container.DataItem, "Ticker") %>' runat="server" /> </ItemTemplate> </asp:Repeater> <asp:Button id=Button1 runat="server" Text="Button1" onmouseover="this.style.backgroundColor='yellow'" onmouseout="this.style.backgroundColor='buttonface'" onclick="Button1_Click"/>
4.Calendar 可供选择具体日期的日历,有多种选择模式,可以自定义图片链接等格式,以文字作为选择链接,在OnDayRender事件上增加自定义内容 <asp:Calendar id=Calendar1 onselectionchanged="Date_Selected" runat="server" /> <asp:Calendar id=Calendar1 runat="server" ondayrender="Calendar1_DayRender" onselectionchanged="Date_Selected" DayNameFormat="Short" SelectionMode="DayWeekMonth" TodayDayStyle-Font-Bold="True" DayHeaderStyle-Font-Bold="True" OtherMonthDayStyle-ForeColor="gray" SelectedDayStyle-BackColor="#ffcc66" SelectedDayStyle-Font-Bold="True" NextMonthText = "<img src='images/monthright.gif' border=0>" PrevMonthText = "<img src='images/monthleft.gif' border=0>" SelectorStyle-BackColor="#99ccff" SelectWeekText = "<img src='images/selweek.gif' border=0 onmouseover=this.style.backgroundColor='#ffcc66' onmouseout=this.style.backgroundColor='#99ccff'>" SelectMonthText = "<img src='images/selmonth.gif' border=0 onmouseover=this.style.backgroundColor='#ffcc66' onmouseout=this.style.backgroundColor='#99ccff'>"/> void Calendar1_DayRender(object sender, DayRenderEventArgs e) { CalendarDay d = ((DayRenderEventArgs)e).Day; TableCell c = ((DayRenderEventArgs)e).Cell; if (d.IsOtherMonth) { c.Controls.Clear(); } else { try { string Hol = holidays[d.Date.Month][d.Date.Day]; if (Hol != string.Empty) c.Controls.Add(new LiteralControl("<br>" + Hol)); } catch (Exception exc) { Response.Write (exc.ToString()); } } }
5.CheckBox 接受选择输入,一般可用于选择多个区域.还可能引发返回事件. <asp:CheckBox id=Check1 Text="CheckBox 1" runat="server" />
6.CheckBoxList 提供多项选择,以Items集合表示各个选项,可自定义格式 <asp:CheckBoxList ID="Check1" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> <asp:ListItem>Item 4</asp:ListItem> <asp:ListItem>Item 5</asp:ListItem> <asp:ListItem>Item 6</asp:ListItem> </asp:CheckBoxList>
7.DropDownList 提供单选下拉列表,可绑定到静态数组,数据源 <asp:DropDownList id=DropDown1 runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> </asp:DropDownList> void Page_Load(Object Sender, EventArgs E) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add ("IN"); values.Add ("KS"); values.Add ("MD"); values.Add ("MI"); values.Add ("OR"); values.Add ("TN"); DropDown1.DataSource = values; DropDown1.DataBind(); } } <asp:DropDownList id="DropDown1" DataSourceID="SqlDataSource1" DataTextField="au_lname" DataValueField="au_id" runat="server" /> <asp:SqlDataSource id="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:Pubs %>' SelectCommand="select DISTINCT au_id, au_lname from authors" runat="server"/>
8.FileUpload 上传文件至服务器.可以指定上传文件格式.需手动编程调用SaveAs方法上传 <asp:FileUpLoad id="FileUpLoad1" AlternateText="You cannot upload files" runat="server" /> <asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server" /> void Button1_Click(object sender, EventArgs e){ if (FileUpLoad1.HasFile) { //Uncomment this line to Save the uploaded file //FileUpLoad1.SaveAs("C:SomePhysicalPath" + FileUpLoad1.Filename); Label1.Text = "Received " + FileUpLoad1.FileName + " Content Type " + FileUpLoad1.PostedFile.ContentType + " Length " + FileUpLoad1.PostedFile.ContentLength; } else { Label1.Text = "No uploaded file"; } }
9.HiddenField 保存一个不需显示的值,如状态值,不适合保存安全敏感性的数据,ValueChanged事件可用. <asp:HiddenField id=HiddenField1 runat=Server /> void Button1_Click(object sender, EventArgs e){ if (HiddenField1.Value == String.Empty) HiddenField1.Value = "0"; //Increment the hidden field value by 1 HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value)+1).ToString(); Label1.Text = HiddenField1.Value; }
10.HyperLink 链接到另一个页面,支持绑定数据到Text和NavigateUrl属性 <asp:hyperlink id=HyperLink1 runat="server">Go To QuickStart</asp:hyperlink> <asp:Repeater id="MyRepeater" runat="server"> <ItemTemplate> <asp:HyperLink id=HyperLink1 Text='<%# DataBinder.eval_r(Container.DataItem, "StringValue")%>' NavigateUrl='<%# "detailspage_cs.aspx?id=" + HttpUtility.UrlEncode(DataBinder.Eval (Container.DataItem,"StringValue").ToString()) %>' runat="server" /> </ItemTemplate> </asp:Repeater>
11.Image 通过ImageUrl属性显示图片 <asp:Image ID="Image1" ImageUrl="images/cereal1.gif" AlternateText="Healthy Grains" runat="server" />
12.ImageButton 像按钮一样返回命令到服务器,也可返回鼠标位置相关信息,也有各种鼠标效果。 <asp:ImageButton id=Button1 ImageUrl="images/mango.jpg" BorderWidth="2px" onclick="ImageButton1_OnClick" runat="server"/> void ImageButton1_OnClick(object Source, ImageClickEventArgs e) { int x=e.X; int y=e.Y; Label1.Text = "X: " + x.ToString(); Label2.Text = "Y: " + y.ToString(); if ( x >= 60 ) { Label3.Text = "You clicked on the Purple Rain!"; } else { Label3.Text = "You clicked on some Extreme Orange!"; } } <asp:ImageButton id=Button3 runat="server" ImageUrl="images/banana.jpg" onmouseover="this.src='images/mango.jpg';" onmouseout="this.src='images/banana.jpg';" onmouseover="this.className='applyBorder'" onmouseout="this.className='removeBorder'" onclick="Button3_Click" />
13.ImageMap 用于创建有热区的图片,可进行链接及返回命令到服务器. <asp:imagemap id="Buttons" imageurl="hotspot.jpg" alternatetext="Navigate buttons" runat="Server"> <asp:RectangleHotSpot hotspotmode="Navigate" NavigateUrl="navigate1.htm" alternatetext="Button 1" top="30" left="175" bottom="110" right="355"> </asp:RectangleHotSpot> </asp:imagemap> <asp:imagemap id="Buttons" imageurl="hotspot.jpg" alternatetext="Navigate buttons" hotspotmode="Postback" onclick="Buttons_Clicked" runat="Server"> <asp:RectangleHotSpot hotspotmode="Postback" postbackvalue="Button1" alternatetext="Button 1" top="30" left="175" bottom="110" right="355"> </asp:RectangleHotSpot> </asp:imagemap>
14.Label 能在指定位置动态显示文本,可进行数据绑定 <asp:Label id="Label1" Text="Label1" Font-Names="Verdana" Font-Size="10pt" Width="200px" BorderStyle="solid" BorderColor="#cccccc" runat="server"/> <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# eval_r("au_lname") %>' /> </ItemTemplate> </asp:FormView>
15.LinkButton 提供如按钮和超级链接的功能,返回页面到服务器. <asp:LinkButton Text="Click Me!" Font-Names="Verdana" Font-Size="14pt" onclick="LinkButton1_Click" runat="server"/>
16.ListBox 提供单选或多选列表,通过SelectionMode属性变多选.支持数据绑定. <asp:ListBox ID="ListBox1" Width="100px" runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> </asp:ListBox> void Page_Load(Object Sender, EventArgs E) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add ("IN"); values.Add ("KS"); values.Add ("MD"); values.Add ("MI"); values.Add ("OR"); values.Add ("TN"); ListBox1.DataSource = values; ListBox1.DataBind(); } }
17.Literal 用于显示文本,无法应用样式,直接显示内容到页面,可用Mode编辑内容. <asp:Literal ID=Literal1 Text="I am a literal control" runat=server /> <asp:Literal ID=Literal2 Mode=Encode Text="<Some encoded text>" runat=server />
18.MultiView and View MultiView控件作为View控件的容器,View也作为其他控件的容器,可根据不同用户展示不同页面程序. <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> <asp:View ID="View1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><strong> </strong> <asp:Button ID="Button1" runat="server" Text="Button" /></asp:View> <asp:View ID="View2" runat="server"> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://www.asp.net">HyperLink</asp:HyperLink> <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="http://www.asp.net">HyperLink</asp:HyperLink></asp:View> <asp:View ID="View3" runat="server"> <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> </asp:View> </asp:MultiView>
19.Panel 作为其他控件的容器.可用于动态生成控件,显示隐藏控件等. <asp:Panel id="Panel1" runat="server" BackColor="gainsboro" ></asp:Panel> for (int i=1; i<=numlabels; i++) { System.Web.UI.WebControls.Label l = new System.Web.UI.WebControls.Label(); l.Text = "Label" + i.ToString(); l.ID = "Label" + i.ToString(); Panel1.Controls.Add(l); Panel1.Controls.Add(new LiteralControl("<br>")); }
20.PlaceHolder 用于动态加载其他控件的容器,无页面输出,只能向Controls集合中加入其他控件, <asp:PlaceHolder id="Bullseye" runat="server" /> void Page_Load(Object sender, EventArgs e) { HtmlButton clicker = new HtmlButton(); clicker.InnerText = "Button 1"; Bullseye.Controls.Add(clicker); clicker = new HtmlButton(); clicker.InnerText = "Button 2"; Bullseye.Controls.Add(clicker); clicker = new HtmlButton(); }
21.RadioButton 提供选项按钮,以相同组名. <asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /> <asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/> <asp:RadioButton id=Radio3 runat="server" Text="Full" GroupName="RadioGroup1" />
22.RadioButtonList 提供单选列表,可定义列表样式. <asp:RadioButtonList id=RadioButtonList1 runat="server"> <asp:ListItem>Item 1</asp:ListItem> <asp:ListItem>Item 2</asp:ListItem> <asp:ListItem>Item 3</asp:ListItem> </asp:RadioButtonList>
23.Substitution 提供一种新缓存页面机制,缓存页面中插入动态值, MethodName属性要指向一个静态方法,AdRotator控件自动获取这种支持. <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
24.Table,TableRow,and TableCell 动态建立表,Rows集合和Cells集合 <asp:Table id="Table1" Font-Names="Verdana" Font-Size="8pt" CellPadding=5 CellSpacing=0 BorderColor="black" BorderWidth="1" Gridlines="Both" runat="server"/> for (int j=0; j<numrows; j++) { TableRow r = new TableRow(); for (int i=0; i<numcells; i++) { TableCell c = new TableCell(); c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString())); r.Cells.Add(c); } Table1.Rows.Add(r); }
25.TextBox 提供用户输入,通过TextMode改变行为 <asp:TextBox id="Text1" Text="Copy this text to the label" Width="200px" runat="server"/>
26.Wizard 用于收集用户资料的输入向导,可定义出前进后退及显示结果的效果,OnActiveStepChanged和OnFinishButtonClick属性定义行为.也可通过OnNextButtonClick定义跳过步骤.可以自定义向导样式,也要自义模板及创建模板. <asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="0" OnActiveStepChanged="GetFavoriteNumberOnActiveStepIndex"> <WizardSteps> <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1"> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>8</asp:ListItem> <asp:ListItem>9</asp:ListItem> <asp:ListItem>10</asp:ListItem> </asp:DropDownList> </asp:WizardStep> <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2" StepType="Complete"> <asp:Label ID="Label1" runat="server"/> </asp:WizardStep> </WizardSteps> <SideBarStyle Width="75px" VerticalAlign="Top"/> </asp:Wizard>
27.Xml 读出XML文件或 XSL Transform内容,DocumentSource属性指定文件,TransformSource指定XSL转换文件,预加载的XMLDocument可以传递到Document属性 <asp:Xml id="xml1" DocumentSource="people.xml" TransformSource="peopletable.xsl" runat="server" /> XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("people.xml")); XslTransform trans = new XslTransform(); trans.Load(Server.MapPath("peopletable.xsl")); xml1.Document = doc; xml1.Transform = trans;