»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
¼¼ÊõÎĵµ > Web¿ª·¢ > ASP.Net
Ç×ÃܽӴ¥ASP.net(5)
·¢±íÈÕÆÚ£º2003-06-26 00:00:00×÷Õߣº· ³ö´¦£º  

ÏÖÔÚÊÇÒѾ­ÊÇÁ賿2µãÁË£¬Ã»Óа취£¬ÏÖÔÚÖ»ÓÐÕâ¸öʱ¼ä²Å¿ÉÄÜÓжàÓàµÄʱ¼äÀ´Ð´ÕâЩÎÄÕ£¬Ï£ÍûÎÒµÄÕâһϵÁÐÎÄÕÂÄܹ»°ïÖú¸ü¶àµÄÈË.ÎÒÃÇÕâÒ»½ÚÖ÷ÒªÊÇÒª½²DataBind£¬Õâ¸öÔÚASP.netÖÐÊǺÜÖØÒªµÄ¶«¶«£¬¼¸ºõËùÓеĿؼþ¶¼ÐèÒªËüÀ´¿ØÖÆÊý¾ÝµÄ²Ù×÷¡£Ò²¿ÉÒÔ˵ÊÇASP.netµÄÊý¾ÝºËÐÄ¡£

ÎÒÃÇÏÈÀ´¿´Ò»¸ö¼òµ¥µÄÀý×Ó£º

<% @ Page Language="C#" %>

<% @ Import Namespace="System.Data" %>

<Script Language="C#" Runat="Server">

public void Page_Load(Object src,EventArgs e)

{

  //Ê×ÏȽ¨Á¢Ò»¸öÊý×é

  ArrayList arr=new ArrayList();

  arr.Add("·Éµ¶");

  arr.Add("Zsir");

  arr.Add("´ó·ç");

  arr.Add("²¼¶¡");

  arr.Add("ÑǺÀ");

  //½«Êý×éÀ¦°óµ½DropDownList¿Ø¼þÉÏÈ¥

  DDL.DataSource = arr;

  DDL.DataBind();

}

</script>

<html>

<head>

<title></title>

</head>

<body>

<asp:DropDownList id="DDL" runat="server" />

</body>

</html>

×îºóµÄÏÔʾΪ:

  ·Éµ¶ Zsir ´ó·ç ²¼¶¡ ÑǺÀ

ÎÒÃÇÔÚ´úÂëÖпÉÒÔ¿´µ½ÎÒÃǽ¨Á¢ÁËÒ»¸öDropDownList£¬µ«ÊÇËûûÓÐ<asp:ListItem>ÊôÐÔ,¶øÎÒÃÇ´Ó×îºóµÄÏÔʾÖÐÒÀÈ»¿ÉÒÔ¿´µ½ÎÒÃÇËùÁгöµÄÑ¡Ïî¡£

ÕâÀï¾ÍÊÇÎÒÃÇÓÃDataBindµÄ½á¹û£¬ÔÚPage_Load·½·¨ÖÐÎÒÃǽ¨Á¢ÁËÒ»¸öÊý×é(ArrayList),²¢Í¨¹ýDataBind·½·¨½«Õâ¸öÊý×éÀ¦°óµ½ÁËDropDownList¿Ø¼þÖУ¬Ê¹µÃDropDownList×îºóÓÐÊý¾ÝÏÔʾ£º£©£¬ÔõôÑù¶ÔBindÓÐÒ»¶¨¸ÐÐÔÈÏʶÁË°É¡£ÏÂÃæÎÒÃÇ¿ªÊ¼Õýʽ½²½â

ÆäʵDataBind()£¬²»½öÄܶԿؼþ½øÐÐÀ¦°ó£¬¶øÇÒ»¹Äܹ»¶ÔÒ³ÃæÖÐÊôÐÔ£¬·½·¨½øÐÐÀ¦°ó£¬ÉõÖÁÕû¸öÒ³Ã涼¿ÉÒÔÀ¦°ó¡£±ÈÈ磬µ÷ÓÃPage.DataBind()·½·¨»òÕßÖ±½ÓʹÓÃDataBind()£¬ÄÇôÕû¸öÒ³Ã涼½«±»À¦°ó£¬ËùÓеÄÊý¾ÝÈ«ÔÚ¼àÊÓ֮ϡ£ÏÂÃæµÄÀý×Ó£¬ÎÒÃǽ«Ê¹ÓÃDataBind·½·¨À´À¦°óDropDownList£¬²¢»ñµÃÆäÖеÄÊý¾Ý

<% @ Page Language="C#" %>

<% @ Import Namespace="System.Data" %>

<Script Language="C#" Runat="Server">

public void sub_Click(Object sender,EventArgs e)

{

Page.DataBind();

}

</script>

<html>

<head>

<title></title>

</head>

<body>

<form runat=server>

 <asp:DropDownList id="DDL" runat="server" >

 <asp:ListItem>ASP¼¼Êõ</asp:ListItem>

 <asp:ListItem selected>ASP.Net¼¼Êõ</asp:ListItem>

 <asp:ListItem>JSP¼¼Êõ</asp:ListItem>

 <asp:ListItem>PHP¼¼Êõ</asp:ListItem>

 <asp:ListItem>×é¼þ¼¼Êõ</asp:ListItem>

 </asp:DropDownList>

<br>

ÄãÏÖÔÚÑ¡ÔñµÄÊÇ:<font color=red><%# DDL.SelectedItem.Text %></font>Çø

<br>

<asp:Button id="sub" Text="Ìá½»" Type="submit" runat=server OnClick="sub_Click" />

</form>

</body>

</html>

Ö´Ðкó£¬ÎÒÃÇÑ¡ÔñJSP¼¼ÊõÎÒÃǵã»÷"Ìá½»"°´Å¥,¿´µ½Çé¿öÊÇ:

 ASP¼¼Êõ ASP.Net¼¼Êõ JSP¼¼Êõ PHP¼¼Êõ ×é¼þ¼¼Êõ

ÄãÏÖÔÚÑ¡ÔñµÄÊÇ:JSP¼¼ÊõÇø

 ÎÒÃÇ¿´µ½£¬ÄǸöºìÉ«µÄ[JSP¼¼Êõ],ÎÒÃDz¢Ã»ÓÐʹÓÃʲô¿Ø¼þ£¬µ«ÊÇËûÈ´ÄÜÕýÈ·µÄÏÔʾÎÒÃǵÄÑ¡Ôñ½á¹û£¬Õâ¸öÊǾÍÀ¦°óµÄ½á¹û£¬×¢Òâ<%# DDL.SelectedItem.Text %>Õâ¾ä»°£¬ÕýÊÇËüÈÃÎÒÃÇÈ¡µÃÁËÀ¦°óµÄÊý¾Ý¡£Ëü¿´ÉÏÈ¥ÊDz»ÊÇÏñÎÒÃÇÊìϤµÄ<%=...%>Õâ¸öÓï¾ä£¬ËüÃǵÄʹÓ÷½·¨Ïà²î²»¶à£¬Ö»ÊÇ<%=...%>ÊÇÔÚ³ÌÐòÖ´ÐÐʱµ÷Óã¬<%#... %>ÊÇÔÚDataBind()·½·¨Ö®ºó±»µ÷Óá£ÒÔºóÎÒÃÇ»¹Äܾ­³£¿´µ½ËûµÄÉíÓ°£¬ºÇºÇ¡£

½ÓÊÕBindµÄ¿Ø¼þ£¬Ò»°ãÓÐDropDownList,DataList,DataGrid,ListBoxÕâЩ¼¯ºÏÐÔÖʵĿؼþ£¬¶ø±»À¦°óµÄÖ÷ÒªÊÇArrayList(Êý×é),Hashtable(¹þÏ¡±í),DataView(Êý¾ÝÊÓͼ),DataReaderÕâËĸö£¬ÒÔºóÎÒÃǾͿÉÒÔ¶ÔºÅÈë×ù£¬²»»á³öÏÖDataTable±»À¦°óµÄ´íÎóÁË£º£©

½²µ½Bind£¬¾Í²»ÄÜ˵һÏÂDataBinder.Eval()·½·¨.

ÎÒÃÇÔÚʹÓÃDataBind,»ñµÃµÄÊý¾Ý£¬ÏµÍ³»á½«ÆäĬÈÏΪString£¨×Ö·û´®£©£¬Õâ¶ÔÎÒÃÇƽʱµÄÊä³öÏÔʾÌṩÁ˼«´óµÄ·½±ã£¬µ«ÊÇÎÒÃDz¢²»ÊÇÿ´Î¶¼ÐèÒªstringÀàÐÍ£¬ÓÐʱÎÒÃǾÍÐèÒªBoolean,Int32ÕâЩÀàÐÍ¡£´ËʱÎÒÃǾÍÐèҪת»¯ÀàÐÍÁË¡£¿ÉÄÜ´ó¼Ò×îÏÈÏëµ½µÄÊÇString.Format·½·¨£¬ÕâÊÇ×îºÃµÄ£¬µ«ÊÇÓ÷¨¹ýÓÚ·³¡£ËùÒÔ×îºÃ²»ÒªÕâÑùÀ²¡£ÎÒÃÇ¿ÉÒÔÓÃDataBinder.Eval()·½·¨£¬ËûµÄ¸ñʽÊÇ:

DataBinder.Eval(Container.DataItem,"ת»»µÄÀàÐÍ","¸ñʽ")

×îºóÒ»¸ö"¸ñʽ"ÊÇ¿ÉÑ¡µÄ£¬Ò»°ã²»ÓÃÈ¥¹ÜËû£¬Container.DataItemÊÇÀ¦°óµÄÊý¾ÝÏî,"ת»»ÀàÐÍ"Ö¸µÄÊÇInteger,String,BooleanÕâÒ»ÀණÎ÷.

ÓÐÁËËü£¬ÎÒÃÇ´¦ÀíÊý¾Ý¾Í¸ü¼Ó·½±ã

×îºóÎÒÃÇÀ´¿´Ò»¸öÀý×Ó£¬¹ØÓÚDataViewµÄÀ¦°ó£¬ºÇºÇ£¬ÀÏÊÇÓÃDropDownList·³ÁË£¬Õâ´ÎÓÃDataGrid,ÔÚÕâ¸öÀý×ÓÖÐÎÒÃÇ¿ÉÒÔ¿´µ½"±íÊÇÔõÑùÁ¶³ÉµÄ"

<% @ Page Language="C#" %>

<% @ Import Namespace="System.Data" %>

<Script Language="C#" Runat="Server">

public void Page_Load(Object src,EventArgs e)

{

int i;

//½¨Á¢±íÊý¾Ý

DataTable dt=new DataTable();

DataRow dr;

//½¨Á¢ColumnÀý£¬¿ÉÒÔÖ¸Ã÷ÀýµÄÀàÐÍ,ÕâÀïÓõÄÊÇĬÈϵÄstring

dt.Columns.Add(new DataColumn("񅧏"));

dt.Columns.Add(new DataColumn("̞"));

for(i=1;i<8;i++)

{

dr=dt.NewRow();

dr[0]=Int32.ToString(i);

dr[1]="aspcn"+Int32.ToString(i);

dt.Rows.Add(dr);

}

//À¦°ó

DG1.DataSource = new DataView(dt);

DG1.DataBind();

}

</script>

<html>

<head>

<title></title>

</head>

<body>

<asp:DataGrid id="DG1" runat=server align=center HeaderStyle-BackColor="#aaaadd" />

</body>

</html>

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00469