我不知道为什么你认为你的标签是在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