htm11h
htm11h

Reputation: 1779

how to capture the value of a gridview template field

In the RowDataBound event of an ASP.NET Gridview I am trying to read the value of a Label in a Template Field. I would prefer to capture this value in the RowUpdating Event, but for some reason I seem to recall that it is not possible. Here is the ASP...

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

Here is the VB.net code that I am trying to figure out....

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

Any and all help would be greatly appreciated.

thanks,

Upvotes: 1

Views: 13470

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

I'm not sure why you think that your Label is in the EditItemTemplate because you've chosen RowState=DataControlRowState.Edit.

Actually you neeed to check for RowType=DataControlRowType.DataRow. That is required since the first row's RowType is the Header.

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

If you want to get the controls of your EditItemTemplate you need to check aditionally the RowState, for example in your GridView(normally you would use an editable control like TextBox in 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>

in 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

Note that i've changed the ID of the controls to somewhat more readable.

Upvotes: 3

Related Questions