Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.net VB.net error loading GridView

Posted on 2014-01-06
3
Medium Priority
?
700 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
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:
Hiran Desai earned 2000 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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline
Suggested Courses

879 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