Solved

datagrid onclick events

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

631 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