Solved

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

Posted on 2014-01-05
3
2,018 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now