467,886 Members | 1,816 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,886 developers. It's quick & easy.

Use FindControl to find DetailsView BoundField control (vb)

Hello,

I am using the code below to set the values of a DetailsView template field
using FindControl. My question is how would you find a control if its a
Boundfield control?

For example, how would I reference the following BoundField ("NTAccount") in
the Sub dvDetail_PreRender sub? Any help would be appreciated...

<asp:BoundField DataField="NTAccount" HeaderText="NT Account" />

Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
EventArgs) Handles dvDetail.PreRender
If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
CType(Me.dvDetail.FindControl("txtChangeActivityDa te"), TextBox).Text =
Now.ToShortDateString
End If
End Sub

--
Thanks in advance,

sck10
Nov 19 '05 #1
  • viewed: 22826
Share:
5 Replies
You should be able to access the BoundColumns using:

e.Item.Cells[n].Text; //Each cell represents a bound column.

"sck10" wrote:
Hello,

I am using the code below to set the values of a DetailsView template field
using FindControl. My question is how would you find a control if its a
Boundfield control?

For example, how would I reference the following BoundField ("NTAccount") in
the Sub dvDetail_PreRender sub? Any help would be appreciated...

<asp:BoundField DataField="NTAccount" HeaderText="NT Account" />

Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
EventArgs) Handles dvDetail.PreRender
If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
CType(Me.dvDetail.FindControl("txtChangeActivityDa te"), TextBox).Text =
Now.ToShortDateString
End If
End Sub

--
Thanks in advance,

sck10

Nov 19 '05 #2
Hi Sck10,

welcome to ASP.NET newsgroup.
Regarding on the accesing BoundField nested controls in DetailsView
control, since it is autogenerated by runtime and will assign a random id
according to the item index, we can not use FindControl method to locate
the nested control instance. And yes, you'r right, we need to use the
PreRender event since at that time , we can ensure that the Control has
been created and assigned the proper value. For example, when we need to
access the TextBox in BoundField for Edit mode, we can use the following
code in DetailsView's PreRender event:

======================
Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.PreRender
For Each dvr As DetailsViewRow In DetailsView1.Rows
If dvr.Cells.Count = 2 Then
If dvr.Cells(1).Controls.Count > 0 Then
Dim txt As TextBox
txt = dvr.Cells(1).Controls(0)
Response.Write("<br>ID: " & txt.ID)
Response.Write("<br>Text: " & txt.Text)
End If
End If
Next
End Sub
========================

Also, for such scenario that we need to extract Text value or reference the
nested controls, I'd prefer using TemplateFields since that'll provide us
more control on control's properties definition such as ID.....

In addition, when the binded datasource has large column set, the control's
controls structure may get very complex, I suggest you try turning on the
Page's Page level Trace which will display the Page's Control Tree, then we
can get a clear view of the Page and its sub controls control structure.
This can help us easily locate the nested controls throug index...

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "sck10" <sc***@online.nospam>
| Subject: Use FindControl to find DetailsView BoundField control (vb)
| Date: Thu, 3 Nov 2005 13:35:30 -0600
| Lines: 25
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <eS*************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135891
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
|
| I am using the code below to set the values of a DetailsView template
field
| using FindControl. My question is how would you find a control if its a
| Boundfield control?
|
| For example, how would I reference the following BoundField ("NTAccount")
in
| the Sub dvDetail_PreRender sub? Any help would be appreciated...
|
| <asp:BoundField DataField="NTAccount" HeaderText="NT Account" />
|
| Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
| EventArgs) Handles dvDetail.PreRender
| If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
| CType(Me.dvDetail.FindControl("txtChangeActivityDa te"), TextBox).Text
=
| Now.ToShortDateString
| End If
| End Sub
|
| --
| Thanks in advance,
|
| sck10
|
|
|

Nov 19 '05 #3
Hi Sck10,

How are you doing on this issue? Does the suggestion in my last reply helps
a little? If there're anything else we can help, please feel free to post
here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 135026330
| References: <eS*************@TK2MSFTNGP09.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Fri, 04 Nov 2005 07:50:30 GMT
| Subject: RE: Use FindControl to find DetailsView BoundField control (vb)
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <PL**************@TK2MSFTNGXA01.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 88
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:136052
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Sck10,
|
| welcome to ASP.NET newsgroup.
| Regarding on the accesing BoundField nested controls in DetailsView
| control, since it is autogenerated by runtime and will assign a random id
| according to the item index, we can not use FindControl method to locate
| the nested control instance. And yes, you'r right, we need to use the
| PreRender event since at that time , we can ensure that the Control has
| been created and assigned the proper value. For example, when we need to
| access the TextBox in BoundField for Edit mode, we can use the following
| code in DetailsView's PreRender event:
|
| ======================
| Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles DetailsView1.PreRender
| For Each dvr As DetailsViewRow In DetailsView1.Rows
| If dvr.Cells.Count = 2 Then
| If dvr.Cells(1).Controls.Count > 0 Then
| Dim txt As TextBox
| txt = dvr.Cells(1).Controls(0)
| Response.Write("<br>ID: " & txt.ID)
| Response.Write("<br>Text: " & txt.Text)
| End If
| End If
| Next
| End Sub
| ========================
|
| Also, for such scenario that we need to extract Text value or reference
the
| nested controls, I'd prefer using TemplateFields since that'll provide us
| more control on control's properties definition such as ID.....
|
| In addition, when the binded datasource has large column set, the
control's
| controls structure may get very complex, I suggest you try turning on the
| Page's Page level Trace which will display the Page's Control Tree, then
we
| can get a clear view of the Page and its sub controls control structure.
| This can help us easily locate the nested controls throug index...
|
| Hope helps. Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
|
| --------------------
| | From: "sck10" <sc***@online.nospam>
| | Subject: Use FindControl to find DetailsView BoundField control (vb)
| | Date: Thu, 3 Nov 2005 13:35:30 -0600
| | Lines: 25
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| | Message-ID: <eS*************@TK2MSFTNGP09.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.framework.aspnet:135891
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | Hello,
| |
| | I am using the code below to set the values of a DetailsView template
| field
| | using FindControl. My question is how would you find a control if its a
| | Boundfield control?
| |
| | For example, how would I reference the following BoundField
("NTAccount")
| in
| | the Sub dvDetail_PreRender sub? Any help would be appreciated...
| |
| | <asp:BoundField DataField="NTAccount" HeaderText="NT Account" />
| |
| | Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
| | EventArgs) Handles dvDetail.PreRender
| | If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
| | CType(Me.dvDetail.FindControl("txtChangeActivityDa te"),
TextBox).Text
| =
| | Now.ToShortDateString
| | End If
| | End Sub
| |
| | --
| | Thanks in advance,
| |
| | sck10
| |
| |
| |
|
|

Nov 19 '05 #4
Thanks Steven,

My issue is resolved.

sck10
"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:Sc**************@TK2MSFTNGXA02.phx.gbl...
Hi Sck10,

How are you doing on this issue? Does the suggestion in my last reply helps a little? If there're anything else we can help, please feel free to post
here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 135026330
| References: <eS*************@TK2MSFTNGP09.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Fri, 04 Nov 2005 07:50:30 GMT
| Subject: RE: Use FindControl to find DetailsView BoundField control (vb)
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <PL**************@TK2MSFTNGXA01.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 88
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:136052
| NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
|
| Hi Sck10,
|
| welcome to ASP.NET newsgroup.
| Regarding on the accesing BoundField nested controls in DetailsView
| control, since it is autogenerated by runtime and will assign a random id | according to the item index, we can not use FindControl method to locate
| the nested control instance. And yes, you'r right, we need to use the
| PreRender event since at that time , we can ensure that the Control has
| been created and assigned the proper value. For example, when we need to
| access the TextBox in BoundField for Edit mode, we can use the following
| code in DetailsView's PreRender event:
|
| ======================
| Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles DetailsView1.PreRender
| For Each dvr As DetailsViewRow In DetailsView1.Rows
| If dvr.Cells.Count = 2 Then
| If dvr.Cells(1).Controls.Count > 0 Then
| Dim txt As TextBox
| txt = dvr.Cells(1).Controls(0)
| Response.Write("<br>ID: " & txt.ID)
| Response.Write("<br>Text: " & txt.Text)
| End If
| End If
| Next
| End Sub
| ========================
|
| Also, for such scenario that we need to extract Text value or reference
the
| nested controls, I'd prefer using TemplateFields since that'll provide us | more control on control's properties definition such as ID.....
|
| In addition, when the binded datasource has large column set, the
control's
| controls structure may get very complex, I suggest you try turning on the | Page's Page level Trace which will display the Page's Control Tree, then
we
| can get a clear view of the Page and its sub controls control structure.
| This can help us easily locate the nested controls throug index...
|
| Hope helps. Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
|
| --------------------
| | From: "sck10" <sc***@online.nospam>
| | Subject: Use FindControl to find DetailsView BoundField control (vb)
| | Date: Thu, 3 Nov 2005 13:35:30 -0600
| | Lines: 25
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| | Message-ID: <eS*************@TK2MSFTNGP09.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.framework.aspnet:135891
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | Hello,
| |
| | I am using the code below to set the values of a DetailsView template
| field
| | using FindControl. My question is how would you find a control if its a | | Boundfield control?
| |
| | For example, how would I reference the following BoundField
("NTAccount")
| in
| | the Sub dvDetail_PreRender sub? Any help would be appreciated...
| |
| | <asp:BoundField DataField="NTAccount" HeaderText="NT Account" />
| |
| | Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
| | EventArgs) Handles dvDetail.PreRender
| | If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
| | CType(Me.dvDetail.FindControl("txtChangeActivityDa te"),
TextBox).Text
| =
| | Now.ToShortDateString
| | End If
| | End Sub
| |
| | --
| | Thanks in advance,
| |
| | sck10
| |
| |
| |
|
|

Nov 19 '05 #5
Thanks for your followup Sck10,

Please feel free to post here when you need our assistance.
Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "sck10" <sc***@online.nospam>
| References: <eS*************@TK2MSFTNGP09.phx.gbl>
<PL**************@TK2MSFTNGXA01.phx.gbl>
<Sc**************@TK2MSFTNGXA02.phx.gbl>
| Subject: Re: Use FindControl to find DetailsView BoundField control (vb)
| Date: Wed, 9 Nov 2005 14:34:11 -0600
| Lines: 150
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <#e*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP11.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:356790
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thanks Steven,
|
| My issue is resolved.
|
| sck10
|
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:Sc**************@TK2MSFTNGXA02.phx.gbl...
| > Hi Sck10,
| >
| > How are you doing on this issue? Does the suggestion in my last reply
| helps
| > a little? If there're anything else we can help, please feel free to
post
| > here. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | X-Tomcat-ID: 135026330
| > | References: <eS*************@TK2MSFTNGP09.phx.gbl>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: st*****@online.microsoft.com (Steven Cheng[MSFT])
| > | Organization: Microsoft
| > | Date: Fri, 04 Nov 2005 07:50:30 GMT
| > | Subject: RE: Use FindControl to find DetailsView BoundField control
(vb)
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | Message-ID: <PL**************@TK2MSFTNGXA01.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | Lines: 88
| > | Path: TK2MSFTNGXA01.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:136052
| > | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
| > |
| > | Hi Sck10,
| > |
| > | welcome to ASP.NET newsgroup.
| > | Regarding on the accesing BoundField nested controls in DetailsView
| > | control, since it is autogenerated by runtime and will assign a random
| id
| > | according to the item index, we can not use FindControl method to
locate
| > | the nested control instance. And yes, you'r right, we need to use the
| > | PreRender event since at that time , we can ensure that the Control
has
| > | been created and assigned the proper value. For example, when we need
to
| > | access the TextBox in BoundField for Edit mode, we can use the
following
| > | code in DetailsView's PreRender event:
| > |
| > | ======================
| > | Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e
As
| > | System.EventArgs) Handles DetailsView1.PreRender
| > | For Each dvr As DetailsViewRow In DetailsView1.Rows
| > | If dvr.Cells.Count = 2 Then
| > | If dvr.Cells(1).Controls.Count > 0 Then
| > | Dim txt As TextBox
| > | txt = dvr.Cells(1).Controls(0)
| > | Response.Write("<br>ID: " & txt.ID)
| > | Response.Write("<br>Text: " & txt.Text)
| > | End If
| > | End If
| > | Next
| > | End Sub
| > | ========================
| > |
| > | Also, for such scenario that we need to extract Text value or
reference
| > the
| > | nested controls, I'd prefer using TemplateFields since that'll provide
| us
| > | more control on control's properties definition such as ID.....
| > |
| > | In addition, when the binded datasource has large column set, the
| > control's
| > | controls structure may get very complex, I suggest you try turning on
| the
| > | Page's Page level Trace which will display the Page's Control Tree,
then
| > we
| > | can get a clear view of the Page and its sub controls control
structure.
| > | This can help us easily locate the nested controls throug index...
| > |
| > | Hope helps. Thanks,
| > |
| > | Steven Cheng
| > | Microsoft Online Support
| > |
| > | Get Secure! www.microsoft.com/security
| > | (This posting is provided "AS IS", with no warranties, and confers no
| > | rights.)
| > |
| > |
| > |
| > | --------------------
| > | | From: "sck10" <sc***@online.nospam>
| > | | Subject: Use FindControl to find DetailsView BoundField control (vb)
| > | | Date: Thu, 3 Nov 2005 13:35:30 -0600
| > | | Lines: 25
| > | | X-Priority: 3
| > | | X-MSMail-Priority: Normal
| > | | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| > | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| > | | Message-ID: <eS*************@TK2MSFTNGP09.phx.gbl>
| > | | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | | NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| > | | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
| > | | Xref: TK2MSFTNGXA01.phx.gbl
| > | microsoft.public.dotnet.framework.aspnet:135891
| > | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | |
| > | | Hello,
| > | |
| > | | I am using the code below to set the values of a DetailsView
template
| > | field
| > | | using FindControl. My question is how would you find a control if
its
| a
| > | | Boundfield control?
| > | |
| > | | For example, how would I reference the following BoundField
| > ("NTAccount")
| > | in
| > | | the Sub dvDetail_PreRender sub? Any help would be appreciated...
| > | |
| > | | <asp:BoundField DataField="NTAccount" HeaderText="NT Account" />
| > | |
| > | | Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
| > | | EventArgs) Handles dvDetail.PreRender
| > | | If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
| > | | CType(Me.dvDetail.FindControl("txtChangeActivityDa te"),
| > TextBox).Text
| > | =
| > | | Now.ToShortDateString
| > | | End If
| > | | End Sub
| > | |
| > | | --
| > | | Thanks in advance,
| > | |
| > | | sck10
| > | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by sck10 | last post: by
3 posts views Thread by Jason | last post: by
7 posts views Thread by studio60podcast | last post: by
2 posts views Thread by Ben | last post: by
reply views Thread by le0 | last post: by
2 posts views Thread by David Lozzi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.