Solved

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

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

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

733 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