Solved

datagrid onclick events

Posted on 2003-11-21
5
29,285 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

744 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

13 Experts available now in Live!

Get 1:1 Help Now