MouseOver on GridView to display an Image

Hi all,

I have a GridView which displays information about Cars. I would like to display Car images on each row of GridView whenever a user mouse overs the Make or Model Fields in the GridView. Below is the GridView:

<asp:GridView ID="carlistGridView" runat="server" AutoGenerateColumns="False" Font-Size="Small"  
        CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="6"
        Width="600px" onrowdatabound="carlistGridView_RowDataBound">
        <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
        <Columns>
            <asp:ImageField DataImageUrlField="ImagePath" HeaderText="Company Name" >
            </asp:ImageField>
            <asp:BoundField DataField="Make" HeaderText="Car Make" />
            <asp:BoundField DataField="Model" HeaderText="Car Model" />
            <asp:BoundField DataField="Size" HeaderText="Car Size" />
            <asp:ButtonField CommandName="ChooseCars" Text="Choose >>" ButtonType="Button"  />
                                                                     
        </Columns>
 
      </asp:GridView>

Any ideas?

Thanks for your help.
Itudk_2010Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jorge_torizConnect With a Mentor Research & Development ManagerCommented:
You can change this line of code:

var newElement = $('<table id=\'' + tableId + '\'><tr><td align=\'right\'><span style=\'cursor:pointer;\' onclick="removeTable(\'' + tableId + '\')">Close</span></td></tr><tr><td><img style=\'width: 200px;\' src=\'' + imgSrc + '\' /></td></tr></table>');

The change is in the style that is applied to the image element
0
 
robastaCommented:
this is similar to what you are looking for: http://www.ezzylearning.com/tutorial.aspx?tid=2861497
0
 
Itudk_2010Author Commented:
Hi robasta,

I tried this example but unsuccessful. It gives me a web service 500 error.

Any other ideas?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jorge_torizResearch & Development ManagerCommented:
You could use something like this:

Header:

    <script language="javascript" type="text/javascript" src="js/jquery.js"></script>
    <script language="javascript" type="text/javascript">
        function showImage(ctl) {
            $('#' + ctl).css('display', 'block');
        }
        function hideImage(ctl) {
            $('#' + ctl).css('display', 'none');
        }
    </script>

Body:

        <asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvImages_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Provider">
                    <ItemTemplate>
                        <div id="divProvider" runat="server"><%# Eval("ProviderName") %></div>
                        <img id="imgTest" runat="server" src='<%# Eval("ImageURL") %>' style="display: none;" />                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable tblProviders = new DataTable("tblProviders");
            tblProviders.Columns.Add("ImageURL", typeof(string));
            tblProviders.Columns.Add("ProviderName", typeof(string));

            tblProviders.Rows.Add(new object[] { "http://www.google.com/images/logos/ps_logo2.png", "Google" });
            tblProviders.Rows.Add(new object[] { "http://www.blackberrysites.com/wp-content/uploads/2009/09/yahoo-finance-logo.png", "Yahoo!" });

            tblProviders.AcceptChanges();
            gvImages.DataSource = tblProviders;
            gvImages.DataBind();
        }
    }
    protected void gvImages_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HtmlGenericControl)e.Row.FindControl("divProvider")).Attributes.Add("onmouseover", "showImage('" + e.Row.FindControl("imgTest").ClientID + "')");
            ((HtmlGenericControl)e.Row.FindControl("divProvider")).Attributes.Add("onmouseout", "hideImage('" + e.Row.FindControl("imgTest").ClientID + "')");
        }
    }
0
 
Itudk_2010Author Commented:
Hi jorge_toriz,

Thanks for the code. Do I have to add the JQuery libraries in Visual Studio? If yes what version? I am using .NET framework 3.5 and Visual Studio 2008.
Looking forward to your reply.
0
 
Itudk_2010Author Commented:
Also you forgot to send me the file: src="js/jquery.js". Is it in the JQuery library?
0
 
jorge_torizResearch & Development ManagerCommented:
Yes, is the jquery library.    The version I used is v1.4.2
jquery.js
0
 
Itudk_2010Author Commented:
Hi jorge_toriz,

Thanks for the code. I tried your example but it doesn't display the image on mouseover, it just displays the cross sign when I mouse over on the field called "Make". I would like to display a particular Car Image when I mouseover on the "Make" field in the GridView.  Could you take a look at the following code.
I have just added some code in the page_load, the rest of the code is your.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = CarRental.BusinessComponents.DataAccess.DBHelper.GetSqlConnection;
            con.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("SELECT  Make, Model, Category, CarImage,   HourlyCharges FROM Car", con);
            da.Fill(ds, "Record");
            gvImages.DataSource = ds;
            gvImages.DataBind();

        }
}

protected void gvImages_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HtmlGenericControl)e.Row.FindControl("divProvider")).Attributes.Add("onmouseover", "showImage('" + e.Row.FindControl("imgTest").ClientID + "')");
            ((HtmlGenericControl)e.Row.FindControl("divProvider")).Attributes.Add("onmouseout", "hideImage('" + e.Row.FindControl("imgTest").ClientID + "')");
        }
    }

aspx page:

<head runat="server">
    <title></title>
   
    <script src="Scripts/jquery.js" type="text/javascript"></script>
     
    <script language="javascript" type="text/javascript">
        function showImage(ctl) {
            $('#' + ctl).css('display', 'block');
        }
        function hideImage(ctl) {
            $('#' + ctl).css('display', 'none');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvImages_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Provider">
                    <ItemTemplate>
                        <div id="divProvider" runat="server"><%# Eval("Make")%></div>
                        <img id="imgTest" runat="server" src='<%# Eval("CarImage") %>' style="display: none;" />                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

any ideas?
Looking forward to your reply.

Thanks
0
 
jorge_torizResearch & Development ManagerCommented:
Verify if the value of your column CarImage has the exact location of your images
0
 
Itudk_2010Author Commented:
Should I provide the whole path of the Image here in the following code?
 <img id="imgTest" runat="server" src='<%# Eval("CarImage") %>' style="display: none;" />                        
   
I am storing the CarImage path in the database and the actual images are stored in the Images folder in the root of the web application. In the databse the CarImage column contains: ~/Images/imageofcar.jpg.
any ideas?              
0
 
jorge_torizResearch & Development ManagerCommented:
Can you send me the HTML that results after the execution?, I want to see the the value of the src attribute
0
 
Itudk_2010Author Commented:
It works now, it displays the car image when I mouseover it. But I want the image as a popup. Right now it displays the image but the width and height of the row in GridView becomes wide. I did use Ajax PopupExtender control before but it did not work. Is it possible with the above code you sent me or is there any way I can make it a popup image when mouseover on the row without increasing the width and height of the GridView row?

Thanks again for your help. Looking forward to your reply
0
 
jorge_torizResearch & Development ManagerCommented:
Mmm... you could do it with absolute positioning... let me try to build a sample for your
0
 
Itudk_2010Author Commented:
Thanks a lot. waitng for your reply.
0
 
jorge_torizResearch & Development ManagerCommented:
I wrote an example that uses only HTML, you need to modify it to accomodate to your solution:

Head:

<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $('span.spanWithImage').mousemove(mouseMove);
    });
    function removeTable(tableId) {
        $('#' + tableId).remove();
    }
    function mouseMove(e) {
        var spanId = $(e)[0].currentTarget.id;
        var tableId = spanId + '_table';
        if ($('#' + tableId).length == 0) {
            var imgSrc = $(e)[0].currentTarget.img;
            var newElement = $('<table id=\'' + tableId + '\'><tr><td align=\'right\'><span style=\'cursor:pointer;\' onclick="removeTable(\'' + tableId + '\')">Close</span></td></tr><tr><td><img src=\'' + imgSrc + '\' /></td></tr></table>');
            newElement.css({
                'position': 'absolute',
                'left': e.pageX,
                'top': e.pageY
            });
            newElement.prependTo($($(e)[0].currentTarget.parentNode));
        }
    }
</script>

Body:

<span class="spanWithImage" id="lblGoogle" img="http://www.google.com/images/logos/ps_logo2.png" style="border-style: solid; border-width: 1px;">Google</span>
0
 
Itudk_2010Author Commented:
Hi jorge_toriz,

Thanks for your time and help. It shows the image much bigger, how is it possible to make the popup image smaller? I don't know that much JavaScript or JQuery but I will try my best to make the changes.
I will let you know when it works.

Thanks again for your help.
0
 
Itudk_2010Author Commented:
Hi jorge_toriz,

Thanks a lot for your help, it works fine.

   
0
All Courses

From novice to tech pro — start learning today.