Solved

datagrid onclick events

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

12 Experts available now in Live!

Get 1:1 Help Now