2010-01-25 42 views
1

我有兩個按鈕在列表視圖中調整該項目的位置,基本上,將其向上移動或向下移動。兩個按鈕都有CommandName="Select",所以我需要知道他們的ID是否在EventArgs中,以便我可以判斷是按下了向上還是向下按鈕。有沒有辦法從ListView SelectedIndexChanged的EventArgs中獲取選擇按鈕的ID?

這是我的臨時sol'n,也許它會讓你更好地瞭解我的意思。

int s; 
    public void iBtnUp_Click(object sender, EventArgs e) 
    { 
     s = 1; 
    } 

    public void iBtnDown_Click(object sender, EventArgs e) 
    { 
     s = 0; 
    } 

    public void lvSteps_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DataKey currentDataKey = this.lvSteps.DataKeys[ lvSteps.SelectedIndex ]; 

     int ID  = (int)currentDataKey.Values[0]; 
     int Step = (int)currentDataKey.Values[1]; 

     clsProcessStep newProcessStep = new clsProcessStep() 
     { 
      ProcessStepID = ID, 
      StepNumber  = Step 
     }; 

     newProcessStep.swapSteps(s); 
    } 

回答

1

隨着CommandName,您可以指定CommandArgument上的按鈕。如果您爲每個按鈕應用一個唯一的命令參數,那麼在您的事件處理程序中,您可以確定哪個按鈕被點擊。

但是,如果使用SelectedIndexChanged事件,則不起作用。這是因爲EventArgs參數中不包含命令名稱和參數詳細信息。您可能需要切換到OnItemCommand處理程序。它在簽名中使用ListViewCommandEventArgs,所以您可以從中獲取CommandName和CommandArgument屬性。

實施例的標記:

<asp:ListView ID="myListView" runat="server" OnItemCommand="myListView_ItemCommand" ... /> 

和相應的後臺代碼:

protected void myListView_ItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    //e.CommandName gives you the command name specified on the button 
    //e.CommandArgument gives you the command argument specified on the button 
} 

下面是對ListView ItemCommand Event MSDN文檔。

希望這可能會幫助你一點。

+0

在此事件發生之前指數是否發生變化?所以我可以得到按鈕被按下的當前選定索引。 – Justen 2010-01-25 18:25:28

+0

我不記得了。一個簡單的方法是調試並在每個處理程序上放置一個斷點(項目命令和選定的索引已更改)。如果您需要在兩個事件處理程序之間傳遞或「記住」值,則可以輕鬆地聲明一個類級變量並在第一個事件期間設置它,在第二個事件期間檢索它。你可能會得到一些好的資源.. – 2010-01-27 02:01:12

1

難道你不只是使用CommandParameter來區分兩者嗎?即一個按鈕的CommandParameter =「Up」,另一個按鈕的「Down」。

編輯

下面是使用CommandParameter的例子,但我現在看到,它可能不適合你現有的代碼。也許你可以將這兩種方法結合起來。

<asp:ListView runat="server" ID="ListView1" OnItemCommand="ListView1_ItemCommand" OnSelectedIndexChanging="ListView1_SelectedIndexChanging"> 
      <LayoutTemplate> 
       <table runat="server" id="table1" runat="server"> 
        <tr runat="server" id="itemPlaceholder"> 
        </tr> 
       </table> 
      </LayoutTemplate> 
      <ItemTemplate> 
       <tr runat="server"> 
        <td id="Td1" runat="server"> 
         <p> 
          <asp:Label ID="Label1" Text="Item" runat="server"></asp:Label> 
          <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument="Up" 
           Text="Up" /> 
          <asp:Button ID="Button2" runat="server" CommandName="Select" CommandArgument="Down" 
           Text="Down" /> 
         </p> 
        </td> 
       </tr> 
      </ItemTemplate> 
      <SelectedItemTemplate> 
       <tr id="Tr2" runat="server"> 
        <td id="Td2" runat="server"> 
         <p style="background-color:Red;"> 
          <asp:Label ID="Label1" Text="Item" runat="server"></asp:Label> 
          <asp:Button ID="Button1" runat="server" CommandName="Select" CommandArgument="Up" 
           Text="Up" /> 
          <asp:Button ID="Button2" runat="server" CommandName="Select" CommandArgument="Down" 
           Text="Down" /> 
         </p> 
        </td> 
       </tr> 
      </SelectedItemTemplate> 
     </asp:ListView> 

和代碼隱藏:

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
       ListView1.DataSource = new List<int>() { 1, 2, 3 }; 
       ListView1.DataBind(); 
      } 
     } 

     public void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) 
     { 
      if (e.CommandName == "Select") 
      { 
       var isGoingUp = (e.CommandArgument.ToString() == "Up"); 
      } 
     } 

     protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) 
     { 
      this.ListView1.SelectedIndex = e.NewSelectedIndex; 


      ListView1.DataSource = new List<int>() { 1, 2, 3 }; 
      ListView1.DataBind(); 
     } 
    } 
+0

嗯,也許?我是網絡開發的新手,所以我不知道我可以設置的所有參數。我將如何檢索該命令參數?我已經向OP添加了一些源代碼,讓您瞭解我現在如何設置它。 – Justen 2010-01-25 14:41:44

相關問題