• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2268
  • Last Modified:

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

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
Murray Brown
Asked:
Murray Brown
  • 2
1 Solution
 
Hiran DesaiSolution ArchitectCommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Thanks very much
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now