 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

October 17th, 2005, 06:46 AM
|
Registered User
|
|
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Access templated controls properties
Hi,
Is it possible to access the properties of a templated control in a GridView, such as an image's height and width properties, so that from code (say a button click) you could dynamically size the image within the Gridview, (and refresh the .aspx page). I am using VB as my preffered language.
Thanks,
Tony
|

October 17th, 2005, 07:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Sure you can. Depending on what you're trying to achieve, and where in the page's rendering state, you can do something like this:
Dim myImage As Image = CType(GridView1.Rows(0).FindControl("Image1"), Image)
This gets a reference to an Image called Image1 in the <ItemTemplate> of the GridView. The trick is to use FindControl for the right control and then cast it to the appropriate type using CType. This is also the way to access controls in, say a FormView....
You can then set the size of the image (or any other property) like this:
myImage.Width = New Unit(400)
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

October 17th, 2005, 09:44 AM
|
Registered User
|
|
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar ... That is exactly what I am looking for, and it NEARLY solves the problem.
An added complication is that the Gridview I am trying to reference is held in a Formview, therefore I need a way to reference the Gridview ie) GridView1 in the first place.
Can I use a findcontrol and variable to get the GridView object, then substitute the variable in the second find control? eg)
Dim MyGrid as GridView = Ctype(Formview1.FindControl("GridView1"),GridView)
Then:
Dim myImage As Image = CType(MyGrid.Rows(0).FindControl("Image1"), Image)
Would this be the right syntax?
I'll try it anyway.
Thank again,
Tony
|

October 17th, 2005, 09:52 AM
|
Registered User
|
|
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
As per my last post, I tried finding the Gridview first with FindControl, and substituting the resulting variable in the second findcontrol, and it worked perfectly.
:D
Thanks again,
Tony
|

October 17th, 2005, 10:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, that's exactly how you should do it. FindControl looks in the current "naming container" which limits the scope of the search operation.
This won't work:
Dim myImage as Image = Ctype(Formview1.FindControl("Image1"),Image)
because the image is not directly embedded in the FormView.
Your syntax to get a reference to the GridView and then use FindControl to find your image is the proper way to do it.
It may seem odd at first, but once you get the hang of it it actually starts making sense.....
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

October 17th, 2005, 10:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
By the way: you can use the same technique to solve your problem from the other post you made on this site.
In the code behind, find the proper control and get its value. Then program against the SelectParameters collection (or a different collection) of your DataSource control and pass it the value dynamically.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |