2011-01-11 1884 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> 

這樣: