简体   繁体   English

listview OnItemCommand没有启动

[英]listview OnItemCommand dosen't fire up

when i press either linkbutton in the listviews it dosen't fire up at all 当我按下列表视图中的任一链接按钮时,它根本不会启动

<div>
   <%
      String[] d1 = { "1", "2", "3" };
      String[] d2 = { "4", "5", "6", "7" };
      ListView1.DataSource = d1;
      ListView1.DataBind();
      ListView2.DataSource = d2;
      ListView2.DataBind();
   %>
   <asp:ListView ID="ListView1" runat="server" OnItemCommand="lv_command">
      <LayoutTemplate>
          <ul>
              <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
          </ul>
      </LayoutTemplate>
      <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
      </ItemTemplate>
   </asp:ListView>
   <asp:ListView ID="ListView2" runat="server" OnItemCommand="lv_command">
      <LayoutTemplate>
          <ul>
              <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
          </ul>
      </LayoutTemplate>
      <ItemTemplate>
          <asp:LinkButton ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
      </ItemTemplate>
   </asp:ListView>
</div>

protected void lv_command(object sender, ListViewCommandEventArgs e)
{
  int i = 0;
}

Set the CommandName property of each of the LinkButtons, for instance: 设置每个LinkBut​​tons的CommandName属性,例如:

 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="MyCommand">LinkButton</asp:LinkButton>

Thus when the ItemCommand event is raised you can detect whether it is fired from a link button as follows: 因此,当引发ItemCommand事件时,您可以按以下方式检测是否从链接按钮中触发该事件:

     protected void lv_command(object sender, ListViewCommandEventArgs e)
    {
  if(e.CommandName == "MyCommand")
  {
    //do something
  }
}

Also it is more performance-wise to bind the listview on initial load only and bind it again from certain event handlers when needed: 同样,在性能上更明智的做法是仅在初始加载时绑定listview,然后在需要时从某些事件处理程序再次绑定它:

    protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
    String[] d1 = { "1", "2", "3" };
    String[] d2 = { "4", "5", "6", "7" };
    ListView1.DataSource = d1;
    ListView1.DataBind();
    ListView2.DataSource = d2;
    ListView2.DataBind();
   }
}

Move the logic that performs data binding into the code-behind: 将执行数据绑定的逻辑移到后面的代码中:

protected void Page_Load(object sender, EventArgs e)
{
    String[] d1 = { "1", "2", "3" };
    String[] d2 = { "4", "5", "6", "7" };
    ListView1.DataSource = d1;
    ListView1.DataBind();
    ListView2.DataSource = d2;
    ListView2.DataBind();
}

protected void lv_command(object sender, ListViewCommandEventArgs e)
{
  int i = 0;
}

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

相关问题 OnItemCommand事件不会在ListView中触发 - OnItemCommand Event Doesn't Fire In ListView 与DataPager绑定时,Repeater Onitem命令不会触发。 为什么? - Repeater Onitemcommand won't fire when binded with DataPager. Why? 在 IsPressed 之后,MenuItem 不会触发 IsHighlighted - MenuItem dosen't fire IsHighlighted after IsPressed FileSystemWatcherChanged事件不会为内存映射文件触发 - FileSystemWatcherChanged event dosen't fire for a memory mapped file OnItemCommand函数不能与Listview中的ASP Linkbutton一起使用 - OnItemCommand function not working with asp Linkbutton in Listview 如何使用转发器的OnItemCommand触发文本框的事件,而不使用LinkBut​​ton? - How to fire event of textbox with OnItemCommand of a repeater , without using LinkButton? OnItemCommand在Telerik Grid上不起作用 - OnItemCommand Doesn't Work on Telerik Grid 如何从ListView的OnItemCommand方法访问当前的ItemType项目值? - How to access the current ItemType Item value from OnItemCommand method of ListView? 防止回发后,ListView不触发OnItemCommand(也不触发ItemInserting) - ListView not firing OnItemCommand (nor ItemInserting) after preventing postback ASP.NET ListView控件未触发OnItemCommand事件 - ASP.NET ListView control not firing OnItemCommand Event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM