ASP.NET基础教程-DataList数据列表控件-选择DataList列表中的一行显示明细信息
2007-10-12 10:41:08
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://chenxing.blog.51cto.com/240526/45966 |
CS文件代码
将DataSet对象设置为当前页的全局静态变量 public class WebForm1 : System.Web.UI.Page { private static DataSet ds; } private void select_data_Click(object sender, System.EventArgs e)
{
//创建数据库连接
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["server"]); //打开数据库
con.Open(); //创建数据适配器对象从数据库读取数据
SqlDataAdapter da=new SqlDataAdapter("select id,name,level,station,dept from verify",con); //声明记录集对象
ds=new DataSet(); //将数据库中提取出的数据加载到记录集
da.Fill(ds,"verify"); //将记录集作为数据列表的数据源
DataList.DataSource=ds; //绑定数据
DataList.DataBind(); //关闭数据库
con.Close(); } private void DataList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e){ if(e.CommandName=="select") {
//设置DataList控件的选中行的索引为当前选择行的索引 DataList.SelectedIndex=e.Item.ItemIndex; DataList.DataSource=ds;//重新赋数据源
DataList.DataBind();//数据绑定 }
} HTML文件代码
<SelectedItemTemplate> 用户名:<FONT face="黑体" size="4"><STRONG> <%# DataBinder.Eval(Container.DataItem,"id") %> </STRONG></FONT><br>
姓 名:<FONT face="黑体" size="4"><STRONG> <%# DataBinder.Eval(Container.DataItem,"name") %></STRONG></FONT><br>
等 级: <FONT face="黑体" size="4"><STRONG> <%# DataBinder.Eval(Container.DataItem,"station") %></STRONG></FONT> <br>
岗 位: <FONT face="黑体" size="4"><STRONG> <%# DataBinder.Eval(Container.DataItem,"level") %></STRONG></FONT><br>
部 门:<FONT face="黑体" size="4"><STRONG> <%# DataBinder.Eval(Container.DataItem,"dept") %></STRONG></FONT><br>
</SelectedItemTemplate> <ItemTemplate>
<asp:Button id="look" runat="server" Text="显示明细" CommandName="select"></asp:Button> <%# DataBinder.Eval(Container.DataItem,"name") %> </ItemTemplate> |


晨星
博客统计信息
热门文章
最新评论
友情链接

