Solved

ASP.net VB.net error loading GridView

Posted on 2014-01-06
3
686 Views
Last Modified: 2014-01-06
Hi

I get the following error when trying to run the following VB.net code on the ASP.net GridView further on

 Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.Image'.

    Private Sub GridView_Questions_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Questions.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            '((Image)e.Row.Controls[ColumnIndex].Controls[ImageControlIndex]).Height = 50;  as we can't assign ID to <asp:ImageField
            ' Use e.Row.DataItem to Grab the Height and Width value
            DirectCast(e.Row.Controls(0).Controls(0), Image).Height = 50

            DirectCast(e.Row.Controls(0).Controls(0), Image).Width = 100
        End If
    End Sub

<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:TemplateField HeaderText="1">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="1">Yes</asp:ListItem>
                         <asp:ListItem Value="0">No</asp:ListItem>      
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>
           
            <asp:TemplateField HeaderText="2">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="1">Yes</asp:ListItem>
                         <asp:ListItem Value="0">No</asp:ListItem>      
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>
         
            <asp:TemplateField HeaderText="3">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="1">Yes</asp:ListItem>
                         <asp:ListItem Value="0">No</asp:ListItem>      
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="4">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList4" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="1">Yes</asp:ListItem>
                         <asp:ListItem Value="0">No</asp:ListItem>      
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="5">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList5" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="1">Yes</asp:ListItem>
                         <asp:ListItem Value="0">No</asp:ListItem>      
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Answer">
                 <ItemTemplate>
                     <asp:RadioButtonList ID="RadioButtonList6" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="0">a</asp:ListItem>
                         <asp:ListItem Value="1">b</asp:ListItem>
                           <asp:ListItem Value="2">c</asp:ListItem>
                         <asp:ListItem Value="3">d</asp:ListItem>
                           <asp:ListItem Value="4">e</asp:ListItem>
                 
                     </asp:RadioButtonList>
                     </ItemTemplate>
            </asp:TemplateField>
         
 
                               <asp:ImageField DataImageUrlField="Image" HeaderText="Image">                    
                    </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
3 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39758923
Hi murbro,

You cann't directly assign height/width values to web control. First you need to declare those control with variable and then assign them with control location.

e.g.  DropDownList mddl = GridView.Rows[2].FindControl("myDropDown1");

Hope this helps.
-Kishan
0
 
LVL 4

Accepted Solution

by:
ItWorked earned 500 total points
ID: 39759072
The problem is with the following line of code which is :
DirectCast(e.Row.Controls(0).Controls(0), Image).Height = 50
DirectCast(e.Row.Controls(0).Controls(0), Image).Width = 100

Open in new window


You need to understand line of code rather then just using it.

//DirectCast(e.Row.Controls(ColumnIndexInWhichYourImageIs).Controls(ControlIndexLevelAtWhichImageIS), Image).Height = 50
DirectCast(e.Row.Controls(7).Controls(0), Image).Height = 50

Open in new window


Hope this will work!
0
 

Author Closing Comment

by:murbro
ID: 39759193
Thanks very much
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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