突然发现这个问题:
xxx.aspx
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate><%# Eval("categoryname") %>[<asp:LinkButton ID="lnk" CommandName="Select" Text="DropComment" runat="server"></asp:LinkButton>]</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnectionstrings %>"
SelectCommand="SELECT * FROM [Category]"></asp:SqlDataSource>
已经声明了OnItemCommand事件,.cs中
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Write(e.CommandName);
}
运行,点击DropComment,并没有显示e.CommandName的值。
去掉Repeater的DataSourceID属性,在.cs中对Repeater进行数据绑定:
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(global.ConnectionString))
{
SqlCommand comm = new SqlCommand("SELECT * FROM [Category]", conn);
conn.Open();
this.Repeater1.DataSource = comm.ExecuteReader(CommandBehavior.CloseConnection);
this.Repeater1.DataBind();
}
}
再运行,点击DropComment,显示"DropComment",说明触发了OnItemCommand事件。
疑问了,为什么用数据源控件时无法触发OnItemCommand事件了?