Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how define demension dynamically

Status
Not open for further replies.

Jefhandle

Technical User
Mar 9, 2005
69
DE
I have on a Default.aspx this tag:
...................
<img src=MyAspx style="height:250px" />

Now i will to make the height of the image dynamically:
<img src= MyAspx style=myParameter />
PS:myParameter is a Parameter, which i retrieve it from database

1-How do this?Where should be myParameter defined, in Default.aspx or in codebehind....?
2-Is There maybe better technique in to define the
Hight instead of to write style="height:250px ,
i mean some other controll, Grid or whatever i dont know them!?

PS:i use C# as to write my Classes, codebehind a.s.o

Thanks
 
try this:

<img src=MyAspx height="<%# DataBinder.Eval(Container.DataItem,"HEIGHT_FIELD_NAME")%>" width="<%# DataBinder.Eval(Container.DataItem,"WIDTH_FIELD_NAME")%>" />



Known is handfull, Unknown is worldfull
 
WIDTH_FIELD_NAME" or HEIGTH_FIELD_NAME", are they in
Codebehind an i change them there? How come they to the Default.aspx?
 
>>myParameter is a Parameter, which i retrieve it from database

wht does this contain?

>>"<%# DataBinder.Eval(Container.DataItem,"HEIGHT_FIELD_NAME")%>"

HEIGHT_FIELD_NAME - refers to a database column name...

Known is handfull, Unknown is worldfull
 
Just use a server image control and set the Width and Height properties from your code behind file.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>>PS:myParameter is a Parameter, which i retrieve it from database

ca8msm:
>>Just use a server image control and set the Width and Height properties from your code behind file

can i have some sample code on how u would do it? the height and width seem to come from the DB...

Known is handfull, Unknown is worldfull
 
You just create your image control e.g.
Code:
<asp:Image ID="Image1" runat="server" />
Then, retrieve the values and set the properties e.g.
Code:
        Image1.Height = myHeightVariable
        Image1.Width = myWidthVariable


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
not that, i wanted to see how u got the values from the DB,
from the post i guess he is loading the image it in a grid...

Known is handfull, Unknown is worldfull
 
Ok, in that case, presumably the poster is returning the height and width as a database field, so you would simply use the ItemDataBound event, get a reference to the image using FindControl and then set the properties. Do you still need an example of this?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
yup, that was wht i was looking for, in the ItemDataBound event how all can u get the data from the source that is being bound to the DataGrid (it could be anything like an array or a DtSet)? do u have a special arguement there? or would u use hidden label fields to get the values from the DB?

e.g.:

<ItemTemplate>
<asp:Label id="ValuesFromDb" runat=server visible=false />
<asp:Img... />
</ItemTemplate>


in ur CB:
-> Find the label, get the value
-> set it to the Img control

OR

is there an event arg to directly read the data from the DataSource (as the ItemDataBound event seems to be called per row)...


Known is handfull, Unknown is worldfull
 
A simple example would be:
Code:
			<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
				<Columns>
					<asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
					<asp:BoundColumn DataField="Height" HeaderText="Height"></asp:BoundColumn>
					<asp:BoundColumn DataField="Width" HeaderText="Width"></asp:BoundColumn>
					<asp:TemplateColumn>
						<ItemTemplate>
							<asp:Image id="Image1" runat="server"></asp:Image>
						</ItemTemplate>
					</asp:TemplateColumn>
				</Columns>
			</asp:DataGrid>

Then, you would just use the Cells attributes to get a reference to the relevant height e.g.
Code:
    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] Page_Load([COLOR=blue]ByVal[/color] sender [COLOR=blue]As[/color] System.Object, [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.EventArgs) [COLOR=blue]Handles[/color] [COLOR=blue]MyBase[/color].Load
        [COLOR=green]'Put user code to initialize the page here[/color]
        DataGrid1.DataSource = CreateDataSource()
        DataGrid1.DataBind()
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]

    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] DataGrid1_ItemDataBound([COLOR=blue]ByVal[/color] sender [COLOR=blue]As[/color] [COLOR=blue]Object[/color], [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.Web.UI.WebControls.DataGridItemEventArgs) [COLOR=blue]Handles[/color] DataGrid1.ItemDataBound
        [COLOR=blue]If[/color] e.Item.ItemType = ListItemType.AlternatingItem [COLOR=blue]Or[/color] e.Item.ItemType = ListItemType.Item [COLOR=blue]Then[/color]

            [COLOR=green]' Find the image control[/color]
            [COLOR=blue]Dim[/color] img [COLOR=blue]As[/color] [COLOR=blue]New[/color] System.Web.UI.WebControls.Image
            img = e.Item.FindControl("Image1")

            [COLOR=green]' Set the attributes[/color]
            img.ImageUrl = "Image" & e.Item.Cells(0).Text & ".jpg"
            img.Height = [COLOR=blue]New[/color] Unit(e.Item.Cells(1).Text, UnitType.Pixel)
            img.Width = [COLOR=blue]New[/color] Unit(e.Item.Cells(2).Text, UnitType.Pixel)
        [COLOR=blue]End[/color] [COLOR=blue]If[/color]
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]

    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] CreateDataSource() [COLOR=blue]As[/color] DataTable
        [COLOR=green]' Simple function to return a DataTable containing 100 rows[/color]
        [COLOR=blue]Dim[/color] dt [COLOR=blue]As[/color] [COLOR=blue]New[/color] DataTable
        [COLOR=blue]Dim[/color] dr [COLOR=blue]As[/color] DataRow
        dt.Columns.Add("ID")
        dt.Columns.Add("Height")
        dt.Columns.Add("Width")
        [COLOR=blue]For[/color] i [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] = 1 [COLOR=blue]To[/color] 10
            dr = dt.NewRow
            dr(0) = i
            dr(1) = i
            dr(2) = i
            dt.Rows.Add(dr)
        [COLOR=blue]Next[/color]
        [COLOR=blue]Return[/color] dt
    [COLOR=blue]End[/color] [COLOR=blue]Function[/color]


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
hmm, which means for any data from the databse can be accessed in CB only by using columns / hidden labels etc right???

Known is handfull, Unknown is worldfull
 
No, you could get it directly from the datasource if you wanted but I can't see why you would need to.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ok, thanx, just wanted to confirm that...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top