我不知道爲什麼你認爲你的標籤是在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更改爲更具可讀性。
那麼,我需要確定所選的行是在編輯模式。由於標題行永遠不會...在其他部分有效,因此我必須對它們進行檢查以確定它們是否需要更改。感謝這個片段,我現在會測試。是的,這不是一個遞歸循環。只需要正在編輯的行的值。 – htm11h 2012-03-01 21:10:07
這可以在RowUpdating事件中完成嗎? – htm11h 2012-03-01 21:17:43
@ marc11h:編輯我的答案。在RowUpdating中,您需要'Dim row = GridView1.Rows(e.RowIndex)'來獲取網格中的更新行。然後你也可以使用'FindControl'來獲得你的conrol的引用。 – 2012-03-01 21:21:06