简体   繁体   English

数据列表项命令事件不起作用

[英]Datalist Itemcommand event is not working

I am trying to work on itemcommand event of datalist. 我正在尝试处理datalist的itemcommand事件。 I want to work with the button that is bind in datalist. 我想使用绑定在数据列表中的按钮。 But when i add a breakpoint on the itemcommand event the event doesnot run.Please tell me why it is not working here is the datalist source and my code 但是当我在itemcommand事件上添加断点时,该事件无法运行。请告诉我为什么它在这里不起作用是数据列表源和我的代码

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" 
            onitemcommand="DataList1_ItemCommand1" >
        <ItemTemplate>
         <ul>
        <li>
    <a href="#"><asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("pictureurl") %>' Width="170px" Height="104px" /></a><br /><br />
        <asp:Label ID="Label1" runat="server" Text='<%#Eval("fullname") %>'></asp:Label><br />
                <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/Icons/accept1.png" CommandName="Accept"   />&nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Icons/decline.png"   />&nbsp;

        </li>
        </ul>
        </ItemTemplate>
        </asp:DataList>

Code: 码:

  if (e.CommandName == "Accept")
        {
            string id = DataList1.DataKeys[e.Item.ItemIndex].ToString() ;

            SqlCommand CmdAcceptRequest = new SqlCommand("Update FriendRequest set requeststatus='Accept' where RequestFrom='" + id + "'", con);
            CmdAcceptRequest.CommandType = CommandType.Text;
            CmdAcceptRequest.ExecuteNonQuery();
            CmdAcceptRequest.Dispose();



         }

This code is not working for me. 此代码对我不起作用。 Item command event is not working Please experts tell me where i am wrong 项目命令事件不起作用请专家告诉我我错了

I assume that you are databinding the DataList on every postback. 我假设您在每次回发中都对DataList进行数据绑定。 Check the IsPostNack property, eg: 检查IsPostNack属性,例如:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         DataBindDataList();
    }
}

private void DataBindDataList()
{
    var dataSource = getSource(); // some data
    DataList1.DataSource = dataSource;
    DataList1.DataBind();
}

Otherwise events won't be triggered if you DataBind a webdatabound control to it's DataSource again. 否则,如果再次将Webdatabound控件DataBind到其DataSource ,则事件将不会触发。 This is true only for manually databound controls not for declarative datasource controls like SqlDataSource or ObjectDataSource . 这仅适用于手动数据绑定控件,而不适用于声明性数据源控件(如SqlDataSourceObjectDataSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM