Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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
 
Old October 17th, 2005, 06:46 AM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old October 17th, 2005, 07:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old October 17th, 2005, 09:44 AM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


 
Old October 17th, 2005, 09:52 AM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old October 17th, 2005, 10:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old October 17th, 2005, 10:44 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding properties to controls pauliehaha ASP.NET 3.5 Basics 0 June 23rd, 2008 04:56 AM
Chapter 6: Templated Controls In Design View geomar BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 1 February 20th, 2007 12:23 PM
Ch 6 Templated Controls geomar BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 2 February 3rd, 2007 07:58 PM
Accessing Properties in User Controls andyj00 ASP.NET 1.0 and 1.1 Professional 16 August 1st, 2005 07:55 AM
default properties on controls Duncan Pro VB.NET 2002/2003 3 November 15th, 2003 02:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.