Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I display an alert on clicking on each row of a GridView?

Thanks in advance...
Posted
Updated 30-Aug-10 0:55am
v4
Comments
The Zetta 30-Aug-10 6:38am    
Windows or Web?
Rohini Deepu 30-Aug-10 6:46am    
web
Ankur\m/ 30-Aug-10 6:56am    
Edit reason: Tagged the question properly.

On GridView add OnRowDataBound handler
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnRowDataBound="grd_OnRowDataBound">


And Code behind

C#
public void grd_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick",String.Format("javascript:alert('You clicked {0}')", e.Row.RowIndex));
        }
    }
 
Share this answer
 
Comments
Rohini Deepu 30-Aug-10 6:49am    
sorry, its not working.
Prosanta Kundu online 30-Aug-10 7:23am    
Show ur sample, which part is not working? What exception it is throwing?
It's RowDataBound event for Gridview and ItemDataBound for Datagrid.

Bind that event and then access each row. This is triggered at the time datasource is binded to grid. Append the Javascript here, like:
C#
e.Row.Attributes.Add("onclick","javascript:alert('Hi')");
 
Share this answer
 
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                cell.Attributes.Add("onclick", "javascript:alert('hai');");
                cell.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900