DataList的事件驱动(在这儿的第一篇技术贴)
准备工作:
1.准备数据源:
1 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HiccPlanConnectionString %>"
2 SelectCommand="select userId,userName,RealName FROM [HiccPlan].[dbo].[aspnet_Users]">
3 </asp:SqlDataSource>
2 SelectCommand="select userId,userName,RealName FROM [HiccPlan].[dbo].[aspnet_Users]">
3 </asp:SqlDataSource>
2.准备DataList:
1<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" DataKeyField="Userid">
2 <ItemTemplate>
3 <asp:Label ID="ff" runat="server"><%# Eval("username") %></asp:Label>
4 <%# Eval("RealName") %>
5 <asp:Button ID="Button1" runat="server" Text="Button" />
6 <asp:Label ID="Label2" runat="server" Text='<%# Eval("UserName")%> '></asp:Label>
7 </ItemTemplate>
8 </asp:DataList>
2 <ItemTemplate>
3 <asp:Label ID="ff" runat="server"><%# Eval("username") %></asp:Label>
4 <%# Eval("RealName") %>
5 <asp:Button ID="Button1" runat="server" Text="Button" />
6 <asp:Label ID="Label2" runat="server" Text='<%# Eval("UserName")%> '></asp:Label>
7 </ItemTemplate>
8 </asp:DataList>
后台代码:
1.开始使用事件驱动:
DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(frank);
2.开始自定义处理过程:
public void frank(object sender, DataListCommandEventArgs e)
{
Label lb=(Label)(e.Item.FindControl("Label2"));
Response.Write( " " + lb.Text);
}
{
Label lb=(Label)(e.Item.FindControl("Label2"));
Response.Write( " " + lb.Text);
}
还可以的用法有:
DataList1.DataKeys[e.Item.ItemIndex].ToString(); //取得DataList1中设置为DataKeyField的字段值.
--------------------------------------------------------------------------------------------------------------------------
来事儿了,先到这里了.