CS文件代码
将DataSet对象设置为当前页的全局静态变量
public class WebForm1 : System.Web.UI.Page
{
private static DataSet ds;
}
将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"]);
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["server"]);
//打开数据库
con.Open();
con.Open();
//创建数据适配器对象从数据库读取数据
SqlDataAdapter da=new SqlDataAdapter("select id,name,level,station,dept from verify",con);
SqlDataAdapter da=new SqlDataAdapter("select id,name,level,station,dept from verify",con);
//声明记录集对象
ds=new DataSet();
ds=new DataSet();
//将数据库中提取出的数据加载到记录集
da.Fill(ds,"verify");
da.Fill(ds,"verify");
//将记录集作为数据列表的数据源
DataList.DataSource=ds;
DataList.DataSource=ds;
//绑定数据
DataList.DataBind();
DataList.DataBind();
//关闭数据库
con.Close();
}
private void DataList_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e){
if(e.CommandName=="select")
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控件的选中行的索引为当前选择行的索引
DataList.SelectedIndex=e.Item.ItemIndex;
DataList.DataSource=ds;//重新赋数据源
DataList.DataBind();//数据绑定
DataList.DataBind();//数据绑定
}
}
}
HTML文件代码
<SelectedItemTemplate>
用户名:<FONT face="黑体" size="4"><STRONG>
<SelectedItemTemplate>
用户名:<FONT face="黑体" size="4"><STRONG>
<%# DataBinder.Eval(Container.DataItem,"id") %> </STRONG></FONT><br>
姓 名:<FONT face="黑体" size="4"><STRONG>
姓 名:<FONT face="黑体" size="4"><STRONG>
<%# DataBinder.Eval(Container.DataItem,"name") %></STRONG></FONT><br>
等 级: <FONT face="黑体" size="4"><STRONG>
等 级: <FONT face="黑体" size="4"><STRONG>
<%# DataBinder.Eval(Container.DataItem,"station") %></STRONG></FONT> <br>
岗 位: <FONT face="黑体" size="4"><STRONG>
岗 位: <FONT face="黑体" size="4"><STRONG>
<%# DataBinder.Eval(Container.DataItem,"level") %></STRONG></FONT><br>
部 门:<FONT face="黑体" size="4"><STRONG>
部 门:<FONT face="黑体" size="4"><STRONG>
<%# DataBinder.Eval(Container.DataItem,"dept") %></STRONG></FONT><br>
</SelectedItemTemplate>
</SelectedItemTemplate>
<ItemTemplate>
<asp:Button id="look" runat="server" Text="显示明细" CommandName="select"></asp:Button>
<%# DataBinder.Eval(Container.DataItem,"name") %>
</ItemTemplate>
<asp:Button id="look" runat="server" Text="显示明细" CommandName="select"></asp:Button>
<%# DataBinder.Eval(Container.DataItem,"name") %>
</ItemTemplate>
转载于:https://blog.51cto.com/chenxing/45966