Kallumasaurus
Kallumasaurus

Reputation: 271

On Click event not firing from code behind

In my code below , my ("OnClick") is not firing. Does anyone know why?

e.Row.Cells(1).Attributes("OnClick") = "window.location.href='MachineSweepLite.aspx?AreaID='" _
   + GridView1.DataKeys(e.Row.RowIndex).Values("ID").ToString()



Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(1).Attributes("onmouseover") = "this.style.cursor='hand';this.style.textDecoration='underline';"
        e.Row.Cells(1).Attributes("onmouseout") = "this.style.textDecoration='none';"
        Dim Index As Integer = e.Row.RowIndex
        e.Row.Cells(1).Attributes("OnClick") = "window.location.href='MachineSweepLite.aspx?AreaID='" + GridView1.DataKeys(e.Row.RowIndex).Values("ID").ToString
    End If
End Sub

Edit above.

Upvotes: 0

Views: 112

Answers (2)

freefaller
freefaller

Reputation: 19953

You have a error in your attribute, as you are adding the value of the ID after the closing single quote...

For example, if the ID was 12, you're sending this to the browser...

window.location.href='MachineSweepLite.aspx?AreaID='12

Note that the 12 is not part of the URL.

You should have the following instead...

e.Row.Cells(1).Attributes("onclick") =
  string.Format("window.location.href='MachineSweepLite.aspx?AreaID={0}';",
  GridView1.DataKeys(e.Row.RowIndex).Values("ID").ToString())

Also note, as of .NET 4.0 it is unnecessary to have the _ character when spanning over multiple lines.

Upvotes: 1

Syed Umar Ahmed
Syed Umar Ahmed

Reputation: 5952

 e.Row.Attributes("onClick") = "CallFunction('" & Convert.ToString(GridView1.DataKeys(e.Row.RowIndex).Values("ID")) & "');"

JS code:

function CallFunction(val)
{
   // do your login here
}

Upvotes: 0

Related Questions