2012-03-01 2 views
1

ASP.NET Gridview의 RowDataBound 이벤트에서 템플릿 필드의 레이블 값을 읽으려고합니다. RowUpdating 이벤트에서이 값을 캡처하는 것을 선호하지만, 어떤 이유로 인해 가능하지 않다는 것을 상기합니다. 여기에 모든 모든 도움을 주시면 감사하겠습니다 여기 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 

ASP는 ...입니다. 당신은 당신이 RowState=DataControlRowState.Edit을 선택했기 때문에 라벨이 EditItemTemplate에 있다고 생각하는 이유

덕분에,

답변

3

잘 모르겠어요.

사실 당신은 RowType=DataControlRowType.DataRow을 확인해야합니다. 이는 첫 번째 행의 RowType이 헤더이기 때문에 필요합니다.

:
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 당신은 (일반적으로 당신이 EditItemTemplate에서 텍스트 상자와 같은 편집 컨트롤을 사용합니다) 당신의 GridView 예를 들면, aditionally RowState를 확인해야의 컨트롤을 얻고 싶다면

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

RowDataBound에서
<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> 

:

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

+0

RowUpdating 이벤트에서이 작업을 수행 할 수 있습니까? – htm11h

+0

@ marc11h : 내 대답을 수정했습니다. RowUpdating에서 그리드의 업데이트 행을 얻으려면'Dim row = GridView1.Rows (e.RowIndex) '가 필요합니다. 그리고'FindControl'을 사용하여 콘트롤에 대한 참조를 얻을 수 있습니다. –

관련 문제