Add javascript in VB.Net and C#


/ Published in: VB.NET
Save to your folder(s)

Hay dos maneras, la mejor es usar el script manager, tambien ver la diferencia entre
http://forums.asp.net/t/902895.aspx


Copy this code and paste it in your HTML
  1. La mejor manera es usar el scriptManager
  2.  
  3. Use the ScriptManager.RegisterStartupScript(). If you take a look at the methods of the ScriptManager class, you will observe that the methods to register client script to the page using the ClientScriptManager class, are also present in the ScriptManager class. So modify the code as shown below:
  4. C#
  5. protected void btnPostback_Click(object sender, EventArgs e)
  6. {
  7. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  8. sb.Append(@"<script language='javascript'>");
  9. sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
  10. sb.Append(@"lbl.style.color='red';");
  11. sb.Append(@"</script>");
  12.  
  13. ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);
  14.  
  15. }
  16. VB.NET
  17. Protected Sub btnPostback_Click(ByVal sender As Object, ByVal e As EventArgs)
  18. Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
  19. sb.Append("<script language='javascript'>")
  20. sb.Append("var lbl = document.getElementById('lblDisplayDate');")
  21. sb.Append("lbl.style.color='red';")
  22. sb.Append("</script>")
  23.  
  24. ScriptManager.RegisterStartupScript(btnPostback,Me.GetType(), "JSCR", sb.ToString(),False)
  25.  
  26. End Sub
  27.  
  28.  
  29.  
  30.  
  31. C#
  32. protected void Page_Load(object sender, EventArgs e)
  33. {
  34. lblDisplayDate.Text = System.DateTime.Now.ToString("T");
  35. }
  36.  
  37. protected void btnPostback_Click(object sender, EventArgs e)
  38. {
  39. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  40. sb.Append(@"<script language='javascript'>");
  41. sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
  42. sb.Append(@"lbl.style.color='red';");
  43. sb.Append(@"</script>");
  44.  
  45. if (!ClientScript.IsStartupScriptRegistered("JSScript"))
  46. {
  47. ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
  48. }
  49. }
  50. VB.NET
  51. Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  52. lblDisplayDate.Text = System.DateTime.Now.ToString("T")
  53. End Sub
  54.  
  55. Protected Sub btnPostback_Click(ByVal sender As Object, ByVal e As EventArgs)
  56. Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
  57. sb.Append("<script language='javascript'>")
  58. sb.Append("var lbl = document.getElementById('lblDisplayDate');")
  59. sb.Append("lbl.style.color='red';")
  60. sb.Append("</script>")
  61.  
  62. If (Not ClientScript.IsStartupScriptRegistered("JSScript")) Then
  63. ClientScript.RegisterStartupScript(Me.GetType(), "JSScript", sb.ToString())
  64. End If
  65. End Sub

URL: http://www.dotnetcurry.com/ShowArticle.aspx?ID=200&AspxAutoDetectCookieSupport=1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.