Solved

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

Posted on 2014-01-05
3
2,078 Views
Last Modified: 2014-01-06
Hi
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">
                <Columns>
                               <asp:ImageField ControlStyle-Height="220"
            ControlStyle-Width="700" DataImageUrlField="Image" HeaderText="Establishment">

                    </asp:ImageField>
         
 
                </Columns>

                <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" />
            </asp:GridView>
0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
ItWorked earned 500 total points
ID: 39758490
Hello,
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">
            <Columns>
                <asp:ImageField DataImageUrlField="tesImage" HeaderText="tesImage" SortExpression="tesImage" />
               
            </Columns>
        </asp:GridView>

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">
            <Columns>                
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Image ImageUrl="<%# ImageColumn %>" Height="<%# HeightColumn%>" Width="<%# WidthColumn%>" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Open in new window


I have tried both solutions and are working fine :)
0
 

Author Closing Comment

by:murbro
ID: 39758711
Great answer. Thanks very much
0
 

Author Comment

by:murbro
ID: 39758860
Hi

I tried your code and got the error shown in the following question
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28331313.html
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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