ListView 的 ItemCommand事件中,找到是 "第幾列"執行的?某一列的索引值? ListViewDataItem的 DisplayIndex屬性 / DataItemIndex屬性

摘要:ListView 的 ItemCommand事件中,找到是 "第幾列"執行的?某一列的索引值?
ListViewDataItem的 DisplayIndex屬性 / DataItemIndex屬性

 

 

剛剛要回答論壇一個問題,

搜尋到 MSDN範例  https://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.listview.updateitem(v=vs.110).aspx

 

發現一個寫法(跟 GridView的 RowIndex不同)

 

Q : ListView 的事件中,找到是 "第幾列"執行的?某一列的索引值?

 

Q : GridView的 RowDataBound事件有 e.Row.RowType & .RowSatae

      ListView的類似事件 (ItemDataBound事件),e.Item.ItemType 沒法 100%對應 ??

 

 

 

****** 這是 MSDN範例 ******

ListView放了一個 Checkbox。     你一打勾,就可以做「資料更新」。
 
 
  protected void PreferredCheckBox_CheckedChanged(object sender, EventArgs e)
  {
    // Gets the CheckBox object that fired the event.
    CheckBox chkBox = (CheckBox) sender;
 
    // Gets the item that contains the CheckBox object. 
    ListViewDataItem item = (ListViewDataItem) chkBox.Parent.Parent;
 
    // Update the database with the changes.
    VendorsListView.UpdateItem(item.DisplayIndex, false);
  }

 

      <asp:ListView ID="VendorsListView" DataSourceID="SqlDataSource1"
        DataKeyNames="VendorID" runat="server">
        <LayoutTemplate>
          Select your preferred vendors:
          <ul runat="server" id="lstVendors" style="text-align:left">
            <li runat="server" id="itemPlaceholder" />
          </ul>
          <asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
 
        <ItemTemplate>
          <li runat="server">
 
            <asp:CheckBox runat="server" ID="PreferredCheckBox" AutoPostBack="True" 
                    Checked='<%# Bind("PreferredVendorStatus") %>'        
                    OnCheckedChanged="PreferredCheckBox_CheckedChanged" />
 
            <asp:Label runat="server" ID="NameLabel" text='<%# Eval("Name") %>' /><br/>
 
          </li>
        </ItemTemplate>
      </asp:ListView>
 
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ...略... />

 

 

****** 這是我自己改的,測試範例,可運作 ******

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
      Button BTN = (Button)e.CommandSource;
 
      ListViewDataItem  LVDItem = (ListViewDataItem)BTN.NamingContainer;
 
 
      Response.Write("<h3>這一列的索引(Index)--" + LVDItem.DataItemIndex + "</h3>" );
 
      Response.Write("<h3>這一列的索引(Index)--" + LVDItem.DisplayIndex + "</h3>");
}
 

如果您要看 GridView的作法,

    請看:[GridView]在 RowCommand事件中,自訂的Button 如何取出某一列(RowIndex) 的索引值#2 -- e.CommandSource

 

 

ListView的寫法,跟GridView有滿多的差異

例如:ListView的 ItemDataBound事件裡面  只有三種 

 

詳見  https://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.listviewitem.itemtype(v=vs.110).aspx

 

ListView使用的ListViewItem.ItemType 屬性,只有這三種  (跟Repeater & DataList使用的 ListItem不一樣喔!)

  • DataItem    ListView 控制項中的資料項目。
  • InsertItem    ListView 控制項中的插入項目。
  • EmptyItem    ListView 控制項中的空白項目。 

    沒有  ItemState .......如 編輯模式(Edit)、格列換色(複數列 Alternating)模式...等等 (GridView有  RowState)

 

 

 

相關文章:

您也可以參考我以前的範例:http://www.dotblogs.com.tw/mis2000lab/Tags/ItemCommand/default.aspx

[GridView]在 RowCommand事件中,自訂的Button 如何取出某一列(RowIndex) 的索引值#2 -- e.CommandSource

[GridView]在 RowCommand事件中,自訂的Button 如何取出某一列(RowIndex) 的索引值#3 -- ButtonField 與 樣板裡面的Button,導致 e.CommandSource寫法不同

 

Will保哥,ListView 的 ItemCommand 事件中如何找到是第幾列執行的

 
 

 
 

 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

線上課程教學,遠距教學 (Web Form 約 51hr)  https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程教學,遠距教學 (ASP.NET MVC 約 135hr)  https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab

 

寫信給我,不要私訊 --  mis2000lab (at) yahoo.com.tw  或  school (at) mis2000lab.net

 (1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A 

 (2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I 

[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm  。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b  


ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。 

.........   facebook社團   https://www.facebook.com/mis2000lab   ......................

.........  YouTube (ASP.NET) 線上教學影片  https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/

 

Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download

請看我們的「售後服務」範圍(嚴格認定)。

...................................................................................................................................................... 

ASP.NET MVC  => .NET Core MVC 線上教學  ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講   事先錄好的影片,並非上課側錄!   觀看時,有如「一對一」面對面講課