• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

click anywhere in gridview asp.net

i'd like to be able to click a row in gridview and send the data from one column in a database to another page.  I'm using aspx and i've tried a bunch of solutions but i can't get the page to response.redirect. with the data from the table.
0
erockman9
Asked:
erockman9
  • 10
  • 10
1 Solution
 
daveamourCommented:
I can help you with this.
Is this for asp.net?  If so which version and which version of Visual Studio?
0
 
erockman9Author Commented:
Visual Studio 2008.
using master pages and web content pages.  it seems like it should be so much easier than it is.  i must be missing something obvious.

i can do it easily with a hyperlink but it needs to be able to work with a click anywhere on the row.

i've attached what i have so far...i was hoping i could just add one more line with a onclick command.

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='FFFF99'")
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='FFFFFF'")

Open in new window

0
 
daveamourCommented:
Ok I think you need to override the Render method like this.  Apologies but this is in C#

protected override void Render(HtmlTextWriter writer)
{
      AddOnClickAttribute();
      base.Render(writer);
}
And add the following method:

private void AddOnClickAttribute()
{
if (gridRS.Rows.Count > 0)
{
foreach (GridViewRow row in gridRS.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gridRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
}
}
}
row.Cells[1] can be changed to use whatever column you need to use to identify the row.
You can then add this method:

protected void gridRS_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowId;
RowId = System.Convert.ToInt32((string)e.CommandArgument);
}
And this gives you the value that was in the Cells[1] and you can do whatever you like with it.
Make sense?
 
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!

 
erockman9Author Commented:
i think i'm close now...
i'm a little unclear on the first part

protected override void Render(HtmlTextWriter writer)
{
      AddOnClickAttribute();
      base.Render(writer);

do i load that in pageload sub in the code behind?
0
 
erockman9Author Commented:
maybe i'm not that close...i must be having a mental block i thought i'd done this before. i'm losing my mind!!!!

i can play with the syntax but do you think my logic is even right?


Partial Public Class Eligibility_CarrierPage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddOnClickAttribute()
    End Sub
    Protected Sub AddOnClickAttribute()
        If (GridView1.Rows.Count > 0) Then
            For Each (GridViewRow row in GridView1.Rows)
            Next
        End If
    End Sub
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Session("TextboxValue") = TextBox1.Text
        Response.Redirect("Eligibility_CarrierSearch.aspx")
    End Sub
 
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='FFFF99'")
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='FFFFFF'")
        e.Row.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + Row.RowIndex.ToString() + "," + row.cells[1].text,true))
    End Sub
 
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim RowId
        RowId = System.Convert.ToInt32((String)e.CommandArgument)
 
    End Sub
End Class

Open in new window

0
 
daveamourCommented:
No, you need to override the Render method and that's where you put AddOnClickAttribute()
Not sure how you override in VB.net?
0
 
erockman9Author Commented:
no..i got that

 Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        AddOnClickAttribute()
    End Sub

now i've got to figure out the rest....

0
 
daveamourCommented:
Ok do the AddOnClickAttribute before the MyBase.Render though
0
 
erockman9Author Commented:
okay...got that!
on the next section i'm not able to add anything beyond this because it doesn't like the word ROW going in there...any ideas

 
Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.EventArgs)
        If (GridView1.Rows.Count > 0) Then
        End If
        For Each (GridViewRow row in gridView1.Rows)
        Next
        If (Row
    End Sub
0
 
daveamourCommented:
AddOnClickAttribute is a method writen by me.
It has no parameters and is not an event handler.
It should juts look like this (In C#):
private void AddOnClickAttribute()
{
 if (gridRS.Rows.Count > 0)
 {
  foreach (GridViewRow row in gridRS.Rows)
  {
   if (row.RowType == DataControlRowType.DataRow)
   row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gridRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
  }
 }
}
0
 
erockman9Author Commented:
i think thats my problem...i should use C## instead of VB...

i made the changes to VB and it looks like this
 
Protected Sub AddOnClickAttribute()
        If (GridView1.Rows.Count > 0) Then
        End If
        For Each Row In GridView1.Rows
        Next

BUT IT FALLS APART AFTER I PUT IN THE NEXT PART

If (e.row.RowType = DataControlRowType.DataRow) Then
         e.row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
        End If
    End Sub

SEE ANYTHING OBVIOUS I'M DOING WRONG????
0
 
daveamourCommented:
Can't see anything obvious but I don't do VB.net.
Is it not compiling?  Any specific error messages?
0
 
erockman9Author Commented:
using
Protected Sub AddOnClickAttribute() i get the error that e is not declared
so then i add the stuff after the () so it looks like this:

(Protected Sub AddOnClickAttribute(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

and then my

  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        AddOnClickAttribute()
        MyBase.Render(writer)

GIVE ME THIS ERROR

Error      1      Argument not specified for parameter 'e' of 'Protected Sub AddOnClickAttribute(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)'.      C:\Users\Eric Hart\Documents\Visual Studio 2008\Projects\WebApplication16\WebApplication16\Eligibility_CarrierSearch.aspx.vb      6      9      WebApplication16


0
 
daveamourCommented:
No you don't need any parameters!
Here is my method:

private void AddOnClickAttribute()
{
 if (gridRS.Rows.Count > 0)
 {
  foreach (GridViewRow row in gridRS.Rows)
  {
   if (row.RowType == DataControlRowType.DataRow)
   row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gridRS, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true));
  }
 }
}
There is no e in there.  You have somehow got an e in your code, that is teh problem.
0
 
erockman9Author Commented:
that seems to get me closer...
except i get name 'e' is not declared...
how do i declare it without the paremeters?




   Protected Sub AddOnClickAttribute()
        If (GridView1.Rows.Count > 0) Then
        End If
        For Each Row In GridView1.Rows
        Next
        If (e.Row.RowType = DataControlRowType.DataRow) Then
         e.row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
        End If
    End Sub
0
 
erockman9Author Commented:
SHOULD I TRY A NEW QUESTION?

i'm not sure i'm making the transition from C## to VB very well.

0
 
daveamourCommented:
No need for a new question, stick with it!
Your first If Statement just opens and closes
Your Foreach has no code within it
Your code has an e in there which has just appeared out of nowhere!
The code below is more like what it shoudl be - may not be quite right but is along the right lines.

   Protected Sub AddOnClickAttribute()
        If (GridView1.Rows.Count > 0) Then
         For Each row In GridView1.Rows
          If (row.RowType = DataControlRowType.DataRow) Then
           row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString() + "," + row.Cells[1].Text, true)
          End If
         Next
        End If
    End Sub
0
 
erockman9Author Commented:
getting close!!!!!!

im getting these errors
Overload resolution failed because no accessible 'GetPostBackEventReference' accepts this number of arguments.      

plus
identifier expected here
row.Cells[1]
0
 
daveamourCommented:
Ok I have coded a complete solution from scratch in VB.net and I don't do VB.net so this was very painful!
Anyway here it is.
 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="EEVBWebApp._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="PeopleGrid" runat="server" OnRowCommand="PeopleGrid_RowCommand"></asp:GridView>
    </div>
    </form>
</body>
</html>
 
 
Partial Public Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            PeopleGrid.DataSource = GetPeopleDataSet()
            PeopleGrid.DataBind()
        End If
    End Sub
 
    Private Function GetPeopleDataSet() As DataSet
        Dim ds As New DataSet()
 
        ds.Tables.Add("People")
        ds.Tables("People").Columns.Add("FirstName")
        ds.Tables("People").Columns.Add("Surname")
        ds.Tables("People").Columns.Add("Age")
        ds.Tables("People").Columns.Add("Location")
 
        ds.Tables("People").Rows.Add("Dave", "Amour", 41, "Rugeley")
        ds.Tables("People").Rows.Add("Fred", "Bloggs", 21, "Cannock")
        ds.Tables("People").Rows.Add("Sue", "smith", 44, "Stafford")
 
        Return ds
    End Function
 
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        AddOnClickAttribute()
 
        MyBase.Render(writer)
    End Sub
 
    Protected Sub AddOnClickAttribute()
        If (PeopleGrid.Rows.Count > 0) Then
            For Each row As GridViewRow In PeopleGrid.Rows
                If (row.RowType = DataControlRowType.DataRow) Then
                    row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(PeopleGrid, "Select$" + row.RowIndex.ToString(), True))
                    row.Attributes.Add("style", "cursor: hand;")
                End If
            Next
        End If
    End Sub
 
    Protected Sub PeopleGrid_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        Response.Write(PeopleGrid.Rows(Convert.ToInt32(e.CommandArgument)).Cells(0).Text)
 
        'You can change the above Cells(0) to Cells(1) for the second column etc
        'You can then use this data to do whatever you want eg go to another Page
        'and maybe pass this in the querystring or whatever you want really
    End Sub
End Class
 
Public Class Person
    Public m_age As Integer
    Private m_name As String
 
    Public Sub New()
 
    End Sub
 
    Public Sub New(ByVal name As String)
        Me.m_name = "Dave"
    End Sub
 
    Public Property Name() As String
        Get
            Return m_name
        End Get
 
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property
 
    Public Property Age() As Integer
        Get
            Return m_age
        End Get
 
        Set(ByVal value As Integer)
            m_age = value
        End Set
    End Property
End Class

Open in new window

0
 
daveamourCommented:
Told you we would sort it!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now