[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Selecting row in datagrid by clicking anywhere

Posted on 2004-10-08
6
Medium Priority
?
329 Views
Last Modified: 2008-02-01
Hopefully a quick and easy one for someone this!!

I want to be able to select a row from a datagrid (and trigger my dg1_ItemCommand -> Select event) when the user clicks anywhere in the row. My code is as follows:

    Private Sub dg1_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg1.ItemCreated
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then
            e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" & "dg1:" & "ctrl" & e.Item.ItemIndex & ":ctrl0','')")
        End If
    End Sub

But it doesn't work! I'm hoping it's something like a typo but I've tried a number of changes and nothing seems to work... It appears to post back but doesn't trigger the code it's supposed to. Incidentally, when someone selects the Select link the following is triggered and works correctly:

    Private Sub dg1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.ui.WebControls.DataGridCommandEventArgs) Handles dg1.ItemCommand
        Select Case e.CommandName
            Case Is = "Select"
                Dim sRowId As String = dg1.DataKeys(e.Item.ItemIndex).ToString()
                Response.Redirect("mypage.aspx?id=" & sRowId, True)
        End Select
    End Sub

Thanks in advance everyone!
0
Comment
Question by:salazie
[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
6 Comments
 
LVL 18

Assisted Solution

by:tusharashah
tusharashah earned 400 total points
ID: 12262755
I just tried similar Code & for some reason I have to add +3 in ItemIndex to select correct row. But the row was selected after that:

e.Item.Attributes.Add( "OnClick", "javascript:__doPostBack('dg1$_ctl" + (e.Item.ItemIndex+3) + "$_ctl5','')" );

-tushar
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12262977
Also..

I had to add an Invisible 'SELECT button' in my DataGrid
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12264879
salazie,
since you have a button to allow user select, then you can solve all the problem at datagrid's onItemCommand event.

Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.ItemCommand
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            'clear previous color
            Dim dgItem As DataGridItem

            For Each dgItem In dg1.Items
                Dim tblClearRow As TableRow
                tblClearRow = CType(dgItem, TableRow)
                If dgItem.ItemType = ListItemType.Item Then
                    tblClearRow.BackColor = Color.FromName("#ccffcc") 'assign item's color
                ElseIf dgItem.ItemType = ListItemType.AlternatingItem Then
                    tblClearRow.BackColor = Color.FromName("#ffffcc") 'assign alternative item's color
                End If
            Next

            'highlight selected row
            Dim tblRow As TableRow
            tblRow = CType(e.Item, TableRow)
            tblRow.BackColor = Color.FromName("#cecece")

            'the rest if you code
            Select Case e.CommandName
                Case Is = "Select"
                             .........
            End Select
        End If
    End Sub

-But, your selected row is located at bottom of the page and when postback occured you may see the page will reload and stay at top of the page. To solve that problem, simply add SmartNavigation="True" in your page directive. After that, your selected row will be retain at the current position as well.
eg:
<%@ Page Language="vb" SmartNavigation="true" AutoEventWireup="false" .....%>

Regards
x_com
0
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!

 
LVL 9

Accepted Solution

by:
hismightiness earned 1600 total points
ID: 12264991
0
 

Author Comment

by:salazie
ID: 12279474
Ok I got it, tusharashah - your suggestion was close but I needed to refer to refer to my web user control as well, this is probably where my problem came in - I imagine yours is directly on a web page. x_com - your solution didn't include any javascript so I'm not sure how this would work but please correct me if I'm wrong...

So the solution was in hismightiness's answer. I've added what I had to do here in case it's of any use to anyone:

    Private Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dg1.ItemCreated
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then
            e.Item.Attributes.Add("onclick", "javascript:__doPostBack('Control1$dg1$_ctl" & (e.Item.ItemIndex + 2).ToString & "$_ctl0','')")
        End If
    End Sub

Thanks everyone!
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12294016
salazie,
Sorry for the delay.. My provided solution is depend on server event. If you prefer client side, it's ok. However, you can enhance your current code if you prefer :
instead of
     e.Item.Attributes.Add("onclick", "javascript:__doPostBack('Control1$dg1$_ctl" & (e.Item.ItemIndex + 2).ToString & "$_ctl0','')")
       
Change to
     e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" & e.item.clientid & "','')")
       
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

650 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