在DataList中监听按钮事件

时间:2011-12-29 16:04:35

标签: c# asp.net web-applications

我正在尝试使用模板字段按钮查看DataList内的一些数据,该按钮会删除所选行的值。但是,当我测试它时,删除按钮删除DataList中的所有值,而不仅删除所选字段。这是我的代码。有谁知道我做错了什么?

protected void Button12_Click(object sender, EventArgs e)
{
    foreach (DataListItem item in DataList2.Items)
    {
        Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("delete_post", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        int post_ID = Convert.ToInt32(post_IDLabel.Text);
        string email = Session["email"].ToString();
        int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
        cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
        cmd.Parameters.Add(new SqlParameter("@myemail", email));
        cmd.Parameters.Add(new SqlParameter("@post_ID", post_ID));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

3 个答案:

答案 0 :(得分:1)

现在,您正在遍历datalist中的整个项目列表(使用foreach循环),因此所有项目都会受到影响。

我认为最简单的方法是处理DataList.ItemCommand事件,并使用绑定表达式将键值传递给代码。

标记:

<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
    <ItemTemplate>
        <asp:Button ID="Button12" runat="server" Text="Button" CommandName="MyDelete" CommandArgument='<%# Eval("MyId") %>' />
    </ItemTemplate>
</asp:DataList>

代码:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if ((e.CommandName == "MyDelete") && (e.CommandArgument != null))
    {
        //Add delete code here using e.CommandArgument to identify the correct record.
    }
}

答案 1 :(得分:1)

尝试这样的事情:

protected void delete(object sender, CommandEventArgs e) 
    {
        if ((e.CommandName == "delete") && (e.CommandArgument != null))
        {
                string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand("delete_post", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                int post_ID = (int) e.CommandArgument;
                string email = Session["email"].ToString();
                int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
                cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
                cmd.Parameters.Add(new SqlParameter("@myemail", email));
                cmd.Parameters.Add(new SqlParameter("@post_ID", post_ID));
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                DataList2.DataBind();

        }

答案 2 :(得分:0)

我想你想要像

这样的东西
var item = DataList2.Items.SelectedItem??

或者如果您可以在您要查找的项目的ID的发件人中传递一些参数

Button btn = (Button)Sender;
int selectedItemId = int.parse(btn.Arguments["Someproperty"]);

var item = DataList2.Items.Single(i => i.Id == selectedItemId);