Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Use FindControl to find DetailsView BoundField control (vb)

Reply
Thread Tools

Use FindControl to find DetailsView BoundField control (vb)

 
 
sck10
Guest
Posts: n/a
 
      11-03-2005
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGF2ZQ==?=
Guest
Posts: n/a
 
      11-03-2005
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
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      11-04-2005
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" <>
| 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: <>
| 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
|
|
|

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      11-09-2005
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: <>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (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: <>
| 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" <>
| | 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: <>
| | 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
| |
| |
| |
|
|

 
Reply With Quote
 
sck10
Guest
Posts: n/a
 
      11-09-2005
Thanks Steven,

My issue is resolved.

sck10


"Steven Cheng[MSFT]" <> wrote in message
news:...
> 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: <>
> | MIME-Version: 1.0
> | Content-Type: text/plain
> | Content-Transfer-Encoding: 7bit
> | From: (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: <>
> | 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" <>
> | | 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: <>
> | | 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
> | |
> | |
> | |
> |
> |
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      11-10-2005
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" <>
| References: <>
<>
<>
| 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: <#>
| 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]" <> wrote in message
| news:...
| > 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: <>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain
| > | Content-Transfer-Encoding: 7bit
| > | From: (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: <>
| > | 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" <>
| > | | 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: <>
| > | | 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
| > | |
| > | |
| > | |
| > |
| > |
| >
|
|
|

 
Reply With Quote
 
UdayGaikwad UdayGaikwad is offline
Junior Member
Join Date: Oct 2011
Posts: 1
 
      10-24-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Findcontrol on Gridview Boundfield?? Type Label? wildman@noclient.net ASP .Net 2 02-14-2008 06:38 PM
how to get a boundfield of a detailsview in javascript? André ASP .Net 0 06-20-2006 07:50 PM
how to get a boundfield of a detailsview in javascript? André ASP .Net Web Controls 0 06-20-2006 07:50 PM
Find Label Control in DetailsView PagerTemplate clintonG ASP .Net Web Controls 1 08-10-2005 02:26 AM
about FindControl ---> to find the control added by program Edward ASP .Net 4 06-14-2004 06:52 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57