2011-01-11 1832 views
0

当我按下在列表视图或者LinkBut​​ton的忽略了最低在所有列表视图OnItemCommand这么想的火起来

<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; 
} 
+0

这对我有用。我假设你的事件处理程序代码在代码隐藏中。您的ListView是否在UpdatePanel中? – 2011-01-11 08:43:10

+0

你能给我一个事件处理程序代码的例子吗? – hhh3112 2011-01-11 08:54:41

回答

2

设置每个了LinkBut​​ton的CommandName属性,例如t被提出,你可以检测它是否是从一个链接按钮如下触发:

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

而且是更多的性能,明智的结合上只有初始加载列表视图,并在需要的时候再从某些事件处理程序绑定它:

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

移动火起来进行数据绑定到代码隐藏的逻辑:

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; 
} 
当ItemCommand甚至

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

这样: