Use FindControl to find DetailsView BoundField control (vb)

S

sck10

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("txtChangeActivityDate"), TextBox).Text =
Now.ToShortDateString
End If
End Sub
 
G

Guest

You should be able to access the BoundColumns using:

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

Steven Cheng[MSFT]

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" <[email protected]>
| 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: <[email protected]>
| 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!TK2MSFTNGP09.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("txtChangeActivityDate"), TextBox).Text
=
| Now.ToShortDateString
| End If
| End Sub
|
| --
| Thanks in advance,
|
| sck10
|
|
|
 
S

Steven Cheng[MSFT]

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: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (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: <[email protected]>
| 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" <[email protected]>
| | 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: <[email protected]>
| | 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!TK2MSFTNGP09.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("txtChangeActivityDate"),
TextBox).Text
| =
| | Now.ToShortDateString
| | End If
| | End Sub
| |
| | --
| | Thanks in advance,
| |
| | sck10
| |
| |
| |
|
|
 
S

sck10

Thanks Steven,

My issue is resolved.

sck10


Steven Cheng said:
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: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (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: <[email protected]>
| 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" <[email protected]>
| | 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: <[email protected]>
| | 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!TK2MSFTNGP09.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("txtChangeActivityDate"),
TextBox).Text
| =
| | Now.ToShortDateString
| | End If
| | End Sub
| |
| | --
| | Thanks in advance,
| |
| | sck10
| |
| |
| |
|
|
 
S

Steven Cheng[MSFT]

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" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| 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: <#[email protected]>
| 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!TK2MSFTNGP11.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
|
|
| | > 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: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: (e-mail address removed) (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: <[email protected]>
| > | 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" <[email protected]>
| > | | 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: <[email protected]>
| > | | 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!TK2MSFTNGP09.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("txtChangeActivityDate"),
| > TextBox).Text
| > | =
| > | | Now.ToShortDateString
| > | | End If
| > | | End Sub
| > | |
| > | | --
| > | | Thanks in advance,
| > | |
| > | | sck10
| > | |
| > | |
| > | |
| > |
| > |
| >
|
|
|
 
Joined
Oct 24, 2011
Messages
1
Reaction score
0
Hi,

If I use 'foreach (DetailsViewRow dvr in DetailsView1)', I came across following error:
foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.DetailsView' because 'System.Web.UI.WebControls.DetailsView' does not contain a public definition for 'GetEnumerator' C:\Users\abc\Default.aspx.cs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top