Link to home
Start Free TrialLog in
Avatar of erockman9
erockman9

asked on

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.
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

I can help you with this.
Is this for asp.net?  If so which version and which version of Visual Studio?
Avatar of erockman9
erockman9

ASKER

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

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?
 
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?
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

No, you need to override the Render method and that's where you put AddOnClickAttribute()
Not sure how you override in VB.net?
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....

Ok do the AddOnClickAttribute before the MyBase.Render though
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
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));
  }
 }
}
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????
Can't see anything obvious but I don't do VB.net.
Is it not compiling?  Any specific error messages?
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


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.
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
SHOULD I TRY A NEW QUESTION?

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

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
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]
ASKER CERTIFIED SOLUTION
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Told you we would sort it!