熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

DataGrid Web控件深度歷險(3) part3

2013-11-13 09:51:51  來源: .NET編程 

  在本文第二部分我們研究了如何通過ButtonColumn標記在DataGrid中顯示按鈕此外我們考察了如何將事件處理程序與按鈕的點擊聯系起來下面我們將了解到如何判斷DataGrid中哪一行的按鈕被點擊並且基於這些信息執行相應的動作
  
  判斷哪一行的按鈕被點擊
  
  回想一下點擊按鈕的事件處理程序定義如下
  
  Sub eventHandlerName(sender as Object e as DataGridCommandEventArgs)
  
  End Sub
  
  DataGridCommandEventArgs類包含一個Item屬性該屬性包含了觸發該事件的源對象Item屬性是TableRow類的一個實例它指向DataGrid中被點擊的那一行可使用Cells屬性訪問TableRow類中的列例如有一個DataGrid它的列信息定義如下
  
  <asp:DataGrid runat=server >
  <Columns>
  <asp:ButtonColumn Text=Details HeaderText=FAQ Details CommandName=details />
  <asp:BoundColumn DataField=FAQID HeaderText=FAQ ID />
  <asp:BoundColumn DataField=Description HeaderText=FAQ Description />
  </Columns>
  </asp:datagrid>
  
  那麼在點擊按鈕的事件處理程序中可通過以下方法獲得被點擊行的某一列的值
  
  Sub detailsClicked(sender as Object e As DataGridCommandEventArgs)
  Dim buttonColumn as TableCell = eItemCells()
  Dim FAQIDColumn as TableCell = eItemCells()
  Dim DescColumn as TableCell = eItemCells()
  
  Dim buttonColText as String = buttonColumnText
  Dim FAQIDColText as String = FAQIDColumnText
  Dim DescColText as String = DescColumnText
  End Sub
  
  示例運行結果如下:
  
  更新按鈕事件處理程序後的DataGrid示例
  
  本示例展示了一個包含Detail按鈕的DataGrid Web控件並演示了當按下按鈕時如何觸發一段代碼注意點擊某個Detail按鈕後你會看到被點擊按鈕所在行的信息
  
  Value of Clicked Button Column Text:
  Value of FAQID Column Text:
  Value of Clicked Description Column Text: How can I format numbers and date/times using ASPNET? For example I want to format a number as a currency
  
  FAQ Details
  FAQ ID
  FAQ Description
  
  
  Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?
  
  
  
  How can I format numbers and date/times using ASPNET? For example I want to format a number as a currency
  
  …
  
  源代碼
  
  <% @Import Namespace=SystemData %>
  <% @Import Namespace=SystemDataSqlClient %>
  <script language=vb runat=server>
  Sub Page_Load(sender as Object e as EventArgs)
  If Not PageIsPostBack then
  BindData()
  End If
  End Sub
  
  
  Sub BindData()
   Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettingsAppSettings(connectionString))
  
   Create the command object passing in the SQL string
  Const strSQL as String = sp_Popularity
  Dim myCommand as New SqlCommand(strSQL myConnection)
  
  Set the datagrids datasource to the datareader and databind
  
  myConnectionOpen()
  dgPopularFAQsDataSource = myCommandExecuteReader(CommandBehaviorCloseConnection)
  dgPopularFAQsDataBind()
  End Sub
  
  
  Sub dispDetails(sender as Object e As DataGridCommandEventArgs)
  Dim buttonColumn as TableCell = eItemCells()
  Dim FAQIDColumn as TableCell = eItemCells()
  Dim DescColumn as TableCell = eItemCells()
  
  Dim buttonColText as String = buttonColumnText
  Dim FAQIDColText as String = FAQIDColumnText
  Dim DescColText as String = DescColumnText
  
  lblBCTText = buttonColText
  lblFCTText = FAQIDColText
  lblDCTText = DescColText
  End Sub
  </script>
  
  <form runat=server>
  <b>Value of Clicked Button Column Text</b>:
  <asp:label id=lblBCT runat=server /><br />
  
  <b>Value of FAQID Column Text</b>:
  <asp:label id=lblFCT runat=server /><br />
  
  <b>Value of Clicked Description Column Text</b>:
  <asp:label id=lblDCT runat=server /><br />
  
  <asp:DataGrid runat=server id=dgPopularFAQs
  BackColor=#eeeeee Width=%
  HorizontalAlign=Center
  FontName=Verdana CellPadding=
  FontSize=pt AutoGenerateColumns=False
  OnItemCommand=dispDetails>
  <HeaderStyle BackColor=Black ForeColor=White FontBold=True HorizontalAlign=Center />
  <AlternatingItemStyle BackColor=White />
  
  <Columns>
  <asp:ButtonColumn Text=Details HeaderText=FAQ Details CommandName=details ButtonType=PushButton />
  <asp:BoundColumn DataField=FAQID HeaderText=FAQ ID />
  <asp:BoundColumn DataField=Description HeaderText=FAQ Description />
  </Columns>
  </asp:datagrid>
  </form>
  
  請仔細檢查上面的示例你可能注意到的第一件事就是按鈕列不包含任何文本這是因為僅需通過HTML即可顯示按鈕因此TableCell的Text屬性返回了一個空字符串
  
  在本文開始部分我講述了一個電子商務公司的場景該公司希望顯示部分貨運信息但同時提供顯示所有貨運信息的選擇到目前為止的示例中我們僅顯示了sp_Popularity存儲過程返回列中的一小部分列想象一下我們僅希望顯示最受歡迎的常見問題的描述列然後提供一個Detail按鈕允許用戶查看某個常見問題的其余信息
  
  雖然我們不希望在DataGrid中顯示FAQID列但是我們仍然需要為detialsClicked事件處理程序提供該信息因為它數據庫中表的關鍵字並唯一標識了每個常見問題通過對DataGrid標記進行小小的改動(在與數據庫中FAQID列對應的BoundColumn標記中增加Visible= False)我們仍然能夠傳遞該信息此改動隱藏了FAQID列但仍然允許detailClicked事件處理程序訪問某個常見問題的標識(通過eItemCells()Text)
  
  因此我們所要做的就是改寫detailsClicked事件處理程序以便當它被觸發時獲得用戶希望顯示的那個常見問題的信息然後再顯示該常見問題的詳細信息在閱讀了一系列關於如何使用DataGrid的文章後當需要顯示數據庫中的數據時你的第一個想法應該就是使用DataGrid因此我們的頁面看起來應該是這樣:
  
  <script language=vb runat=server>
  Sub Page_Load(sender as Object e as EventArgs)
  If Not PageIsPostBack then
  BindData() Only bind the data on the first page load
  End If
  End Sub
  
  
  Sub BindData()
  Make a connection to the database
  Databind the DataReader results to the gPopularFAQs DataGrid
  End Sub
  
  
  Sub detailsClicked(sender as Object e As DataGridCommandEventArgs)
  Get detailed information about the selected FAQ and bind
  the database results to the dgFAQDetails DataGrid
  End Sub
  </script>
  
  <form runat=server>
  <asp:DataGrid runat=server id=dgFAQDetails >
  
  </asp:datagrid>
  
  <asp:DataGrid runat=server id=dgPopularFAQs >
  <Columns>
  <asp:ButtonColumn Text=Details HeaderText=FAQ Details
  ButtonType=PushButton />
  <asp:BoundColumn DataField=FAQID Visible=False />
  <asp:BoundColumn DataField=Description HeaderText=FAQ Description />
  </Columns>
  </asp:datagrid>
  </form>
  
  示例運行結果如下:
  
  本示例展示了如何在DataGrid的每一行中顯示概要信息和一個Detail按鈕當按鈕被點擊時對所選擇的數據項顯示其余信息
  
  Category Name
  FAQ Description
  Views
  Author
  Authors Email
  Date Added
  
  Getting Started
  Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?
  
  Scott Mitchell
  mitchel
  
  
  FAQ Details
  FAQ Description
From:http://tw.wingwit.com/Article/program/net/201311/11785.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.