2012-03-01 24 views
1

在ASP.NET Gridview的RowDataBound事件中,我試圖讀取模板字段中的標籤的值。我寧願在RowUpdating Event中捕獲這個值,但由於某種原因,我似乎記得它不可能。這裏是ASP ...如何捕獲gridview模板字段的值

<asp:TemplateField HeaderText="Translation" ItemStyle-Width="250" >      
    <ItemTemplate> 
      <asp:Label ID="Label11" runat="server" Text='<%# Bind("lang_String") %>' Width="250px"></asp:Label> 
    </ItemTemplate> 

這裏是我試圖找出VB.net代碼....

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 

    If (e.Row.RowState And DataControlRowState.Edit) > 0 Then 
     ' The value in the third column postion. 
     Dim needThisValue as string = e.Row.Cells(3).Text 

    End If 
End Sub 

任何和所有幫助將不勝感激。

感謝,

回答

3

我不知道爲什麼你認爲你的標籤是在EditItemTemplate因爲你已經選擇了RowState=DataControlRowState.Edit。其實你需要檢查RowType=DataControlRowType.DataRow。這是必需的,因爲第一行的RowType是標題。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowtype.aspx

Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     ' in the following way you get the row's DataSource: ' 
     Dim row = DirectCast(e.Row.DataItem, DataRowView).Row 
     ' you could also use the DataSource to get the value: ' 
     Dim lang_String = row.Field(Of String)("lang_String") 
     ' and here you get the reference to your Label in the ItemTemplate: ' 
     Dim Label11 = DirectCast(e.Row.FindControl("Label11"), Label) 
     ' at this point Label11.Text is already set to lang_String ' 
    End If 
End Sub 

如果你希望得到您的EditItemTemplate你需要的方法,另外檢查RowState,例如在你的GridView(通常你會使用EditItemTemplate可編輯控制像文本框)的控件:

<ItemTemplate> 
    <asp:Label ID="LblLanguage" runat="server" Text='<%# Bind("lang_String") %>' Width="250px"></asp:Label> 
</ItemTemplate> 
<EditItemTemplate> 
    <asp:TextBox ID="TxtLanguage" runat="server" Text='<%# Bind("lang_String") %>' Width="250px"></asp:TextBox> 
</EditItemTemplate> 
RowDataBound

Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     ' in the following way you get the row's DataSource: ' 
     Dim row = DirectCast(e.Row.DataItem, DataRowView).Row 
     ' you could also use the DataSource to get the value: ' 
     Dim lang_String = row.Field(Of String)("lang_String") 

     If e.Row.RowState = DataControlRowState.Normal Then 
      ' and here you get the reference to your Label in the ItemTemplate: ' 
      Dim LblLanguage = DirectCast(e.Row.FindControl("LblLanguage"), Label) 
      ' at this point LblLanguage.Text is already set to lang_String ' 
     ElseIf e.Row.RowState = DataControlRowState.Edit Then 
      ' and here you get the reference to your TextBox in the EditItemTemplate: ' 
      Dim TxtLanguage = DirectCast(e.Row.FindControl("TxtLanguage"), TextBox) 
      ' at this point TxtLanguage.Text is already set to lang_String ' 
     End If 
    End If 
End Sub 

請注意,我已將控件的ID更改爲更具可讀性。

+0

那麼,我需要確定所選的行是在編輯模式。由於標題行永遠不會...在其他部分有效,因此我必須對它們進行檢查以確定它們是否需要更改。感謝這個片段,我現在會測試。是的,這不是一個遞歸循環。只需要正在編輯的行的值。 – htm11h 2012-03-01 21:10:07

+0

這可以在RowUpdating事件中完成嗎? – htm11h 2012-03-01 21:17:43

+0

@ marc11h:編輯我的答案。在RowUpdating中,您需要'Dim row = GridView1.Rows(e.RowIndex)'來獲取網格中的更新行。然後你也可以使用'FindControl'來獲得你的conrol的引用。 – 2012-03-01 21:21:06

相關問題