Answered by:
Get value of a label control inside the DetailsView

Question
-
User-2029388304 posted
Hi everyone,
I have a databound label control (i.e. Label 3) inside an Item Template that's in a DetailsView control. I just need to get the value of those label and compare it to some string. I tried several ways (i.e. Find control, DetailsView.Row) but nothing seems to work. I don't know if I'm using the FindControl methos correctly or if I need to access the Row first and do the findcontrol method.
Protected Sub DetailsView4_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView4.DataBound 'If (DetailsView4.Rows(2).Cells(1).ToString = "Regular") Then 'Label3.ForeColor = Drawing.Color.Azure 'End If 'Dim StatusQualifierContainer As Label 'Dim S As String 'StatusQualifierContainer = DetailsView4.FindControl(Label3.Text) 'S = StatusQualifierContainer.Text 'If S = "Regular" Then 'Label5.ForeColor = Drawing.Color.Aqua 'End If 'Dim C As Label = DetailsView4.FindControl("Label3") 'If C.Text = "Regular" Then 'Label3.ForeColor = Drawing.Color.DarkBlue 'End If 'Dim headerRow As DetailsViewRow = DetailsView4.HeaderRow 'Dim lblText As Label = CType(headerRow.FindControl("Label3"), Label) 'Dim txt As String = lblText.ToString() 'If txt.Contains("Regular") Then 'Label3.ForeColor = Drawing.Color.Blue 'Label3.Visible = False 'End If 'Dim a As DetailsViewRowCollection = DetailsView4.Rows 'Dim lblText As Label = CType(DetailsView4.FindControl("Label3"), Label) 'Dim txt As String = lblText.ToString() 'If txt.Contains("Regular") Then 'Label3.ForeColor = Drawing.Color.Blue 'Label3.Visible = False 'End If Dim A As Label = DetailsView4.FindControl("Label3") Dim B As String = A.Text.ToString() If B = "Regular" Then Label3.ForeColor = Drawing.Color.Blue Label3.Visible = False End If End Sub
<asp:DetailsView ID="DetailsView4" runat="server" AutoGenerateRows="False" DataSourceID="LastPorposalStatusDataSource" cssClass="TableRows" Height="50px" Width="100%" > <FieldHeaderStyle cssClass="TableHeading" Width="250px" /> <AlternatingRowStyle CssClass="TableAlternateRows" Width="100%"/> <RowStyle CssClass="TableRowStyle" /> <Fields> <asp:TemplateField SortExpression="Location of proposal"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("[Location of proposal]") %>'></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("[Location of proposal]") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> This proposal was submitted for <asp:Label ID="Label3" runat="server" Text='<%# Bind("[Status Qualifier]") %>'></asp:Label> on <asp:Label ID="Label4" runat="server" Text='<%# Bind("[Status Date]") %>'></asp:Label> . Currently, your proposal is with the <asp:Label ID="Label1" runat="server" Text='<%# Bind("[Location of proposal]") %>'></asp:Label> . According to our system, the proposal is <asp:Label ID="Label2" runat="server" Text='<%# Bind("[Current Status]") %>'></asp:Label> . <asp:Label ID="lblText3" runat="server" Text="Label"></asp:Label> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView>
Many thanks for your inputs. I would greatly appreciate it.Wednesday, September 1, 2010 12:57 PM
Answers
-
User626880745 posted
DetailsView4.FindControl(Label3.Text)Its a TemplateField. FindControl() is what you need to use. for example:
Dim lbl As Label = DirectCast(DetailsView4.FindControl("Label3"), Label)
debug that. see whether you get lbl (as not nothing) and then get the .Text
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 1:13 PM -
User626880745 posted
you're welcome.
It seems to me that DirectCast is used to access the Label control inside the template field. Do I have to use that each time i need to access controls inside the template?Actually its the FindControl() method that is significant here in finding a templated control. the cast is done with TryCast or DirectCast; I won't rant on that one, the documentation should guide you: http://msdn.microsoft.com/en-us/library/zyy863x8(VS.80).aspx (we also have some threads on the differences, I remember one such). FYI, VB.NET does casting implicitly, vs whereas, in C#, the compiler needs it explicitly.
I wonder how I can access the Label (lblText3 in the code above)same way as you found the other control, using FindControl()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 2:09 PM -
User-577741185 posted
You need to cast it to the right control.
Dim lblText3 As Label = DirectCast(DetailsView4.FindControl("Label3"), LabelDim lblText3 As Label = DirectCast(DetailsView4.FindControl("Label3"), Label)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 2:45 PM
All replies
-
User626880745 posted
DetailsView4.FindControl(Label3.Text)Its a TemplateField. FindControl() is what you need to use. for example:
Dim lbl As Label = DirectCast(DetailsView4.FindControl("Label3"), Label)
debug that. see whether you get lbl (as not nothing) and then get the .Text
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 1:13 PM -
User-1491938696 posted
You were very close, I think you had used the wrong name for the label - it should be lblText3 after you have declared the new Label variable. This will work for you:
Dim lblText3 As Label = DetailsView4.FindControl("Label3") lblText3.Text.ToString() If (lblText3.Text = "Regular") Then lblText3.ForeColor = Drawing.Color.Blue End If
Wednesday, September 1, 2010 1:46 PM -
User-2029388304 posted
Hello Peter,
Wow, you're right. I've been searching for answers since yesterday with no luck. Thank you so so much!
I did the code below (I don't know if this is the one you're saying) but I guess it worked because I saw the text color change now.
Dim A As Label = DirectCast(DetailsView4.FindControl("Label3"), Label) If (Not A Is Nothing) Then Dim B As String = A.Text.ToString Select Case B Case "Regular" Label3.ForeColor = Drawing.Color.Blue Case Else End Select End If
However, I have one more clarifications on this one. It seems to me that DirectCast is used to access the Label control inside the template field. Do I have to use that each time i need to access controls inside the template?I wonder how I can access the Label (lblText3 in the code above) I've added to display a particular text if the condition above is true. I can't seem to find how I can drill down to the controls inside that detailsView.
Again, thanks a lot. You've been of great help.
Wednesday, September 1, 2010 1:55 PM -
User626880745 posted
you're welcome.
It seems to me that DirectCast is used to access the Label control inside the template field. Do I have to use that each time i need to access controls inside the template?Actually its the FindControl() method that is significant here in finding a templated control. the cast is done with TryCast or DirectCast; I won't rant on that one, the documentation should guide you: http://msdn.microsoft.com/en-us/library/zyy863x8(VS.80).aspx (we also have some threads on the differences, I remember one such). FYI, VB.NET does casting implicitly, vs whereas, in C#, the compiler needs it explicitly.
I wonder how I can access the Label (lblText3 in the code above)same way as you found the other control, using FindControl()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 2:09 PM -
User-2029388304 posted
Thanks again, Peter!
I will do my research on DirectCast.
Wednesday, September 1, 2010 2:20 PM -
User-577741185 posted
You need to cast it to the right control.
Dim lblText3 As Label = DirectCast(DetailsView4.FindControl("Label3"), LabelDim lblText3 As Label = DirectCast(DetailsView4.FindControl("Label3"), Label)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 1, 2010 2:45 PM