本文介绍了列表视图OnItemCommand这么想的火起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我preSS任的LinkBut​​ton在列表视图忽略了最低都火了起来。

 < D​​IV>
   <%
      串[] D1 = {1,2,3};
      串[] D2 = {4,5,6,7};
      ListView1.DataSource = D1;
      ListView1.DataBind();
      ListView2.DataSource = D2;
      ListView2.DataBind();
   %GT;
   < ASP:ListView控件ID =ListView1的=服务器OnItemCommand =lv_command>
      <&LayoutTemplate模板GT;
          < UL>
              < ASP:占位符ID =itemPlaceholder=服务器/>
          < / UL>
      < / LayoutTemplate模板>
      <&ItemTemplate中GT;
          < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器>的LinkBut​​ton< / ASP:LinkBut​​ton的>
      < / ItemTemplate中>
   < / ASP:的ListView>
   < ASP:ListView控件ID =ListView2=服务器OnItemCommand =lv_command>
      <&LayoutTemplate模板GT;
          < UL>
              < ASP:占位符ID =itemPlaceholder=服务器/>
          < / UL>
      < / LayoutTemplate模板>
      <&ItemTemplate中GT;
          < ASP:LinkBut​​ton的ID =LinkBut​​ton2=服务器>的LinkBut​​ton< / ASP:LinkBut​​ton的>
      < / ItemTemplate中>
   < / ASP:的ListView>
< / DIV>保护无效lv_command(对象发件人,ListViewCommandEventArgs E)
{
  INT I = 0;
}


解决方案

设置每个LinkBut​​tons的CommandName属性,例如:

 < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CommandName =MyCommand>的LinkBut​​ton< / ASP:LinkBut​​ton的>

因此​​,当ItemCommand事件引发你可以检测如下无论是从一个链接按钮触发:

 保护无效lv_command(对象发件人,ListViewCommandEventArgs E)
    {
  如果(e.CommandName ==MyCommand)
  {
    //做一点事
  }
}

此外,它是更多的性能,明智的做法是在绑定只能在初始加载列表视图,并在需要时再从某些事件处理程序绑定:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
   如果(!Page.IsPostBack)
   {
    串[] D1 = {1,2,3};
    串[] D2 = {4,5,6,7};
    ListView1.DataSource = D1;
    ListView1.DataBind();
    ListView2.DataSource = D2;
    ListView2.DataBind();
   }
}

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:

 <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:

     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:

    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();
   }
}

这篇关于列表视图OnItemCommand这么想的火起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:22