Solved

ASP.net VB.net error loading GridView

Posted on 2014-01-06
3
682 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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.

763 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

12 Experts available now in Live!

Get 1:1 Help Now