Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 26th, 2005, 10:35 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataGrid onclick event?


I'm new her and I hope to find someone who can help me in my problem.
My problem is:
I need to get the key value from the selected row in datagrid so I can bind another datagrid from this value.
And I must use onclick event to do this.
Any ideas I'll very thankfull.

Regards
 
Old December 28th, 2005, 02:48 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Can I ask, why must you use onclick?

Jim
 
Old December 28th, 2005, 04:01 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you bind a datagrid, usually you would set the DataKeyField to the database field containing your primary key. When you select a row, you can get at the rows key value via the DataKeys property.

   grid.DataKeys[grid.SelectedIndex]

On what do you wish to click to select the row?

-Peter
 
Old December 29th, 2005, 03:50 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your responces.
Jim: I need to use onclick because the customer wanted to be like that and they refuse to put any buttons in the grid.

Peter: Your code is ok with buttons but her i'm not using any buttons. I wante to click any where in the row to be selected, also the project leader wanted to be OnDoubleClick event :(

Regards

Hani
 
Old December 29th, 2005, 11:38 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

First I would ask the customer why they want no buttons. This seems very stupid to me. It would not be intuative for the user to click or double click a row to do something. Have a button or hyperlink is makes much more sense, and will be more user friendly. Talk to them about it.


Here is a quick example, assuming your ID column is in the first column, calls a function called test() The test function just does a simple alert, but once you have the value you can do whatever with it:
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

   e.Item.Attributes.Add("onDblClick", "test(" + e.Item.Cells(0).Text + ")")

End If
 
Old December 29th, 2005, 05:16 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

One trick I have used is this:

Add a non-visible select column to your grid:

    <asp:buttoncolumn text="Select" commandname="Select" visible="False" />

Then in the codebehind, you can get at the select button in the appropriate cell and assign its client-side event handler code to the row's double click event. You'll have to do that in the page's pre-render event handler because of the way the datagrid control structure is built.

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        For Each objItem As DataGridItem In DataGrid1.Items
            Select Case objItem.ItemType
                Case ListItemType.AlternatingItem, ListItemType.Item
                    objItem.Attributes("ondblclick") = GetPostBackClientEvent(objItem.Cells(0).Controls(0 ), String.Empty)
            End Select
        Next
    End Sub


-Peter
 
Old December 30th, 2005, 12:19 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Cool trick Peter!

 
Old January 1st, 2006, 05:21 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much.

Happy New Year :)

Hani





Similar Threads
Thread Thread Starter Forum Replies Last Post
onclick event bjackman Access 6 July 15th, 2004 06:54 AM
onclick event pigtail Javascript 1 April 11th, 2004 03:10 PM
onClick Event mateenmohd Javascript 4 December 16th, 2003 01:08 AM
onClick event problem mateenmohd Javascript 8 August 2nd, 2003 05:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.