Solved

ASP.net VB.net error loading GridView

Posted on 2014-01-06
3
688 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

809 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