Solved

ASP.net VB.net error loading GridView

Posted on 2014-01-06
3
694 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Murray Brown
ID: 39759193
Thanks very much
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

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…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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