Aspdotnetcodebook’s Weblog

July 21, 2008

How To Add Title Attribute To GridView Row

Filed under: Uncategorized — aspdotnetcodebook @ 5:09 am



Let us suppose that you have a :Grid View with about seven rows across.One of the cells is a five-digit code, for a long name.and your requirement is that the user move the mouse over the code cell, and display this long name in a small popup window, to remind the user of which the code describes.Here is a simple solution without using JavaScript. Just add “Title” attribute to each row to display the information. It will have the same effect as mouse over and mouse out


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Mouseover.aspx.cs"
Inherits="Mouseover" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" OnRowDataBound="GridView1_RowDataBound">
              <Columns>
                  <asp:BoundField DataField="id" HeaderText="ProductId" />
                  <asp:BoundField DataField="Name" HeaderText="Name" />
                  <asp:BoundField DataField="sellPrice" HeaderText="SalePrice" />
                  <asp:TemplateField HeaderText="LongName">
                      <ItemTemplate>
                          <asp:Label ID="LongName" runat="server" Text='<%#ShortDesc(Eval("Desc").ToString()) %>'></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="LongAgencyName" Visible="False">
                      <ItemTemplate>
                          <asp:Label ID="LongName1" runat="server" Text='<%#Eval("Desc")%>'></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
      </div>
  </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mouseover : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           GridView1.DataSource = GetData();
           GridView1.DataBind();

       }
   }
   public DataSet GetData()
   {
       DataSet ds = new DataSet();
       DataTable dt = new DataTable("Movie");
       DataRow dr;
       dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
       dt.Columns.Add(new DataColumn("Name", typeof(string)));
       dt.Columns.Add(new DataColumn("SellPrice", typeof(Int32)));
       dt.Columns.Add(new DataColumn("Desc", typeof(string)));
       for (int i = 1; i <= 10; i++)
       {
           dr = dt.NewRow();
           dr[0] = i;
           dr[1] = "Name" + i.ToString();
           dr[2] = (i * 3)+i*i;
           dr[3] = "Abcdefghijklmnopqwrstuvwxyz"+i.ToString();
           dt.Rows.Add(dr);
       }
       ds.Tables.Add(dt);
       Session["dt"] = dt;
       return ds;
   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
     {
         Label lbl = (Label)e.Row.FindControl("LongName1");
      e.Row.Attributes.Add("Title", lbl.Text);
      e.Row.Attributes["style"] = "Cursor:hand";
     }

   }
   public string ShortDesc(string strDesc)
   {

       return strDesc.Substring(0, 5);

   }
}

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.