簡體   English   中英

On Click事件未從背后的代碼觸發

[英]On Click event not firing from code behind

在下面的代碼中,我的("OnClick")未觸發。 有人知道為什么嗎?

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

在上方編輯。

您的屬性有誤,因為您要單引號添加ID的值...

例如,如果ID為12 ,則將其發送到瀏覽器...

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

請注意, 12不是URL的一部分。

您應該改為以下內容...

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

另請注意,從.NET 4.0開始,跨越多行時不必具有_字符。

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

JS代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM