<asp:DropDownList ID="myicon" runat="server">
<asp:ListItem Value="1">001</asp:ListItem>
<asp:ListItem Value="2">002</asp:ListItem>
<asp:ListItem Value="3">003</asp:ListItem>
</asp:DropDownList>
代码如上:
由于有几十个头像,如何做一个循环代替中间的语句。
<asp:ListItem Value="1">001</asp:ListItem>
<asp:ListItem Value="2">002</asp:ListItem>
<asp:ListItem Value="3">003</asp:ListItem>
</asp:DropDownList>
代码如上:
由于有几十个头像,如何做一个循环代替中间的语句。
{
myicon.Items.Add(...);
}
int i;
for (i=1; i< 24;i++)
Response.Write("<option value=face/"+i+".gif>face"+i+"</option>");
Response.Write("</select> ");
Response.Write("<IMG id=face src=face/"+(i-23)+".gif border=0>");
%>
string[] strTemp = new string[]{"标题1","标题2","标题3"};
myiconDropDownList.DatatSource = strTemp;
myiconDropDownList.DataBind();
//代码片段结束
========================
建议:不用通过Response.Write方法来写。把要显示的记录生成数组,或直接通过SQL查询后返回的DataTable来绑定。
事例:
//代码片段2:查询数据库把记录显示在<asp:DropDownList>里
/*注释:
ddYqlj4为<asp:DropDownList>
bTemp.rtRs方法返回的是通过查询SQL语句得到的DataTable,原始定义为:
public DataTable rtRs(string sqlStr){}
*/
ddYqlj4.DataSource = bTemp.rtRs("SELECT NewTitle, LinkUrl FROM NewsInfo WHERE (WZFlag = 'R4') ORDER BY AddDate");
ddYqlj4.DataTextField = "NewTitle"; //绑定显示的文字为新闻标题
ddYqlj4.DataValueField = "LinkUrl";//绑定该ITEM的值为新闻的连接地址
ddYqlj4.DataBind();
//代码片段结束
========================