Javascript code not being called in asp.net code behind
I have a block of code (posted below) where if the first IF clause is satisfied, the app does not call the javascript('MyPortfolioItemExists()') function. Instead, it exits the function and goes on to process other code lines.
If drPortfolio.HasRows Then
Dim p As Page = CType(System.Web.HttpContext.Current.Handler, Page)
p.ClientScript.RegisterStartupScript(Me.GetType(), "Scri开发者_如何学Pythonpt", "javascript:'MyPortfolioItemExists()';", True)
Return ""
Exit Function
ElseIf drFav.HasRows = False And drPortfolio.HasRows = False Then
Utils.ExecNonQuery("insert into UserPortfolio values ('" & PortfoName & "','" & PortfoPage & "','" & Username & "')")
Return GeneratePortfolioContent()
End If
How can I force the javascript function to be executed?
p.ClientScript.RegisterStartupScript just registers the script to be executed on the client. See the documentation for more information on this function.
You can't execute Javascript on the server (unless, of course, you are writing the server-side in Javascript which you are not). Figuring out the difference between server side code and client side code is something many beginners have gotten hung up on and WebForms blurs the line even more.
精彩评论