I seem to be asking a lot questions today! I have a very simple problem, but I'm still only learning VB so I can't find the right way to code it:
I have an asp:Image within a FormView control. It is databound and pulls up an image as required from a querystring parameter. I would like to check as the page is loading what the dimensions of the image being displayed is, and if it is above a certain
amount, to manually set a width property to the control.
I am not sure how to access this control in my code.
This is a classic scenario. You can access the control by using MyFormView.FindControl("Image_ControlID"), which you will then need to cast to type Image:
Dim imgCtl As Image = DirectCast(MyFormView.FindControl("Image1"), Image)
Replace FormView1 and Image1 with the respective ID's. Also, make sure that the image is in the curent template that the formview is displaying.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Great! Thanks for that, I can now pick out the control and get the values I need.
Only problem is I am getting an error when trying to use > operator on the width property. The error says the width is a unit data type. Is there a way to get this into a integer or something?
I thought I should add my code to help explain:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim LargeImage As Image = DirectCast(FormView1.FindControl("Image1"), Image)
Dim Width As Integer
Width = LargeImage.Width.Value
If Width > 560 Then
LargeImage.Width = 560
End If
End Sub
I was hoping this would resize any images over 560px wide, but it doesn't take any affect.
The key to figuring that out is to right-click and view the source, check out what's being rendered to the browser. If you see the width being applied, then your setting is correct, but the browser isn't "listening" to it.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
And the image wasn't being found, an exception would be thrown.
Are you binding the formview after page load, because if you are, that would wipe away any of your changes.
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
I thought that might do it, as I had put code in page load event. I have tried moving it into the form view event at various places. However, it still seems to ignore it.
The one place it throws an error is if I place the following code, so it runs as the form view databinds (I think):
Protected Sub FormView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBinding
Dim LargeImage As Image = DirectCast(FormView1.FindControl("Image1"), Image)
Dim Width As Integer
Width = LargeImage.Width.Value
If Width > 560 Then
LargeImage.Width = Unit.Pixel(560)
End If
End Sub
The error reads:
Object reference not set to an instance of an object.
Line 13: Width = LargeImage.Width.Value
magicjoef
Member
410 Points
271 Posts
Accessing a control that is in a formview
Jul 23, 2008 06:09 PM|LINK
I seem to be asking a lot questions today! I have a very simple problem, but I'm still only learning VB so I can't find the right way to code it:
I have an asp:Image within a FormView control. It is databound and pulls up an image as required from a querystring parameter. I would like to check as the page is loading what the dimensions of the image being displayed is, and if it is above a certain amount, to manually set a width property to the control.
I am not sure how to access this control in my code.
Any help much appreciated.
Thanks,
Joe
asp:image width resize image control
mellamokb
Contributor
4061 Points
724 Posts
Re: Accessing a control that is in a formview
Jul 23, 2008 06:15 PM|LINK
Hello,
This is a classic scenario. You can access the control by using MyFormView.FindControl("Image_ControlID"), which you will then need to cast to type Image:
Dim imgCtl As Image = DirectCast(MyFormView.FindControl("Image1"), Image)Cheers,
~ mellamokb
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Accessing a control that is in a formview
Jul 23, 2008 06:15 PM|LINK
Hey,
Image imageControl = (Image)FormView1.FindControl("Image1");
Replace FormView1 and Image1 with the respective ID's. Also, make sure that the image is in the curent template that the formview is displaying.
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
magicjoef
Member
410 Points
271 Posts
Re: Accessing a control that is in a formview
Jul 23, 2008 09:31 PM|LINK
Great! Thanks for that, I can now pick out the control and get the values I need.
Only problem is I am getting an error when trying to use > operator on the width property. The error says the width is a unit data type. Is there a way to get this into a integer or something?
I thought I should add my code to help explain:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim LargeImage As Image = DirectCast(FormView1.FindControl("Image1"), Image)
Dim Width As Integer
Width = LargeImage.Width.Value
If Width > 560 Then
LargeImage.Width = 560
End If
End Sub
I was hoping this would resize any images over 560px wide, but it doesn't take any affect.
Thanks,
Joe
mellamokb
Contributor
4061 Points
724 Posts
Re: Accessing a control that is in a formview
Jul 23, 2008 11:42 PM|LINK
Hi,
For Unit types, you just need to use the builtin methods provided by the Unit class:
Cheers,
~ mellamokb
magicjoef
Member
410 Points
271 Posts
Re: Accessing a control that is in a formview
Jul 24, 2008 08:37 AM|LINK
Cheers, I have updated my code to include this but it still isn't working.
I'm not sure my code is actaully affecting the image in the formview once I've checked it?
Any more pointers would be great.
Thanks
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Accessing a control that is in a formview
Jul 24, 2008 01:20 PM|LINK
Hey,
The key to figuring that out is to right-click and view the source, check out what's being rendered to the browser. If you see the width being applied, then your setting is correct, but the browser isn't "listening" to it.
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
magicjoef
Member
410 Points
271 Posts
Re: Accessing a control that is in a formview
Jul 24, 2008 01:39 PM|LINK
Thanks, I have tried that and no width property is making it through.
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Accessing a control that is in a formview
Jul 24, 2008 03:44 PM|LINK
Hey,
OK, looking at this logically, if you do:
Dim Width As Integer = LargeImage.Width.Value
And the image wasn't being found, an exception would be thrown.
Are you binding the formview after page load, because if you are, that would wipe away any of your changes.
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
magicjoef
Member
410 Points
271 Posts
Re: Accessing a control that is in a formview
Jul 25, 2008 10:57 AM|LINK
Thanks for the reply.
I thought that might do it, as I had put code in page load event. I have tried moving it into the form view event at various places. However, it still seems to ignore it.
The one place it throws an error is if I place the following code, so it runs as the form view databinds (I think):
Protected Sub FormView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBinding
Dim LargeImage As Image = DirectCast(FormView1.FindControl("Image1"), Image)
Dim Width As Integer
Width = LargeImage.Width.Value
If Width > 560 Then
LargeImage.Width = Unit.Pixel(560)
End If
End Sub
The error reads:
Object reference not set to an instance of an object.
Line 13: Width = LargeImage.Width.Value