ASP.net VB.net Set dimensions of image field in GridView

Posted on 2014-01-05
Medium Priority
Last Modified: 2014-01-06
I have an ASP.net GridView with an image field where the dimensions are set
Is it possible to store these dimensions in two of my SQL table
columns as I have the DataImageURL?

            <asp:GridView ID="GridView_Questions" runat="server" BackColor="White"
                BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
                Height="147px" Visible="False" Width="795px">
                               <asp:ImageField ControlStyle-Height="220"
            ControlStyle-Width="700" DataImageUrlField="Image" HeaderText="Establishment">


                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="White" Font-Bold="False" ForeColor="#000066" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />
Question by:Murray Brown
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

ItWorked earned 2000 total points
ID: 39758490
I'm posting two possible solution for doing this...
1) using <asp:ImageField >

Your ASP Grid code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
            EmptyDataText="There are no data records to display." OnRowDataBound="GridView1_RowDataBound">
                <asp:ImageField DataImageUrlField="tesImage" HeaderText="tesImage" SortExpression="tesImage" />

Open in new window

Code in .Cs File for RowDataBound Event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
               //((Image)e.Row.Controls[ColumnIndex].Controls[ImageControlIndex]).Height = 50;  as we can't assign ID to <asp:ImageField
               // Use [b]e.Row.DataItem[/b] to Grab the Height and Width value
                ((Image)e.Row.Controls[0].Controls[0]).Height = 50;
                ((Image)e.Row.Controls[0].Controls[0]).Width = 100;

Open in new window

2) using <asp:TemplateField>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
            EmptyDataText="There are no data records to display." OnRowDataBound="GridView1_RowDataBound">
                        <asp:Image ImageUrl="<%# ImageColumn %>" Height="<%# HeightColumn%>" Width="<%# WidthColumn%>" />

Open in new window

I have tried both solutions and are working fine :)

Author Closing Comment

by:Murray Brown
ID: 39758711
Great answer. Thanks very much

Author Comment

by:Murray Brown
ID: 39758860

I tried your code and got the error shown in the following question

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question