Solved

datagrid onclick events

Posted on 2003-11-21
5
29,312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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