Solved

datagrid onclick events

Posted on 2003-11-21
5
29,294 Views
Last Modified: 2012-08-14
hi I have an issue with onclick event on a datagrid cell, after the data grid is populated is there any way i could know which row is clicked on the client side???
0
Comment
Question by:raj3005
5 Comments
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9803121
This example selects the row that was clicked on if you put it in a mouseup event.

You can get the CurrentRowIndex of the datagrid.  Does this help?

myDataGrid.Select(myDataGrid.CurrentRowIndex);

Good luck!

Tony
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9803139
My example is from a Windows form.  Are you doing a Windows or ASP app?
0
 

Author Comment

by:raj3005
ID: 9803566
tony its an asp page
0
 
LVL 1

Expert Comment

by:darkxenn
ID: 9811262
ya, there is... your call to the onclick event should look something like this
myDG_OnClick(Object sender, DataGridCommandEventArgs E)
{

// E is now a the row that has been selected in the datagrid
//this will now create a textbox, so you can copy the 4th counting column in, where i have put for the sake of argument a textbox... this textbox that you have made is now the box that is in the column that was selected, and is the 4th thing in

Dim txtLastName As textbox = E.Item.cells(3).Controls(0)
}


i hope this makes sense... if not, you can always visit this... best site out there for asp.net questions on datagrids, in my opinion


http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=dgEdit

hope it helps,

Xen
0
 

Accepted Solution

by:
Aania earned 125 total points
ID: 9813048
Hello,
I'm not quite sure what you'd like to do with the row information once you have it client side, but in addition to what Xen has already said, you can use the "OnItemDataBound" option of your datagrid to bind attributes to buttons and other controls such as javascript events (mouseOver/mouseOut/onClick...)
In this example, I've made an image button that looks like a recycle bin, and prompts a user whether or not they'd really like to send the item to the recycle bin before doing so. If the user clicks "No", the statement is terminated, if they click "Yes", it runs the server side command assigned to the image button (in this case the "delete" command) -- but you can obviously replace this code with any relevant javascript, and the control doesn't necessarily need a server side command and attached to it.

in the ASP portion:
<asp:datagrid OnItemDataBound="myDataGrid_DataBound" ... />

The Image Button (in a template column) - still in the ASP portion:

<ASP:IMAGEBUTTON ImageUrl="siteImages/icons/20/recycle.gif" BorderWidth="0" CommandName="Delete" Runat="server" id="recycle"></ASP:IMAGEBUTTON>

// Alternately, if you don't need it to run a serverside command, you can remove 'CommandName="Delete"'

in the C# code:
// This code runs with every row that is added to the datagrid **including** the header / footer & paging - the if statement makes sure that you're not looking for the control where it doesn't exist (ie. the header)
public void myDataGrid_DataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
         if (e.Item.ItemType == ListItemType.Item |  e.Item.ItemType == ListItemType.AlternatingItem |  e.Item.ItemType == ListItemType.EditItem)
         {
                   ImageButton myRecycleButton = new ImageButton();
                   myRecycleButton = (ImageButton)e.Item.FindControl("recycle");
                   myRecycleButton.Attributes.Add("onclick", "return confirm('Are you Sure you want to remove the folder " + System.Convert.ToString(DataBinder.Eval(e.Item.DataItem, "name")).Replace("'", "\\'") + " and send all of its contents to the recycle bin?');");
         }
}

And that's it. You can replace
System.Convert.ToString(DataBinder.Eval(e.Item.DataItem, "name"))
with whichever column in your datagrid that you like -- if you are using javascript though, be careful to replace potentially invalid characters with their literals. The Replace("'", "\\'") portion will result in "Vanessa\'s Folder" in the javascript code, which will read properly ("Vanessa's Folder") in the prompt (the second backslash gets consumed by the C#).

I hope this helps someone,

Vanessa

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now