Return to Snippet

Revision: 34660
at October 26, 2010 23:39 by ramirocalero


Updated Code
La mejor manera es usar el scriptManager

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:
C#
protected void btnPostback_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");
 
    ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);
 
}
VB.NET
Protected Sub btnPostback_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
      sb.Append("<script language='javascript'>")
      sb.Append("var lbl = document.getElementById('lblDisplayDate');")
      sb.Append("lbl.style.color='red';")
      sb.Append("</script>")
 
      ScriptManager.RegisterStartupScript(btnPostback,Me.GetType(), "JSCR", sb.ToString(),False)
 
End Sub




C#
protected void Page_Load(object sender, EventArgs e)
{
    lblDisplayDate.Text = System.DateTime.Now.ToString("T");
}
 
protected void btnPostback_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");
 
    if (!ClientScript.IsStartupScriptRegistered("JSScript"))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
    }
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      lblDisplayDate.Text = System.DateTime.Now.ToString("T")
End Sub
 
Protected Sub btnPostback_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
      sb.Append("<script language='javascript'>")
      sb.Append("var lbl = document.getElementById('lblDisplayDate');")
      sb.Append("lbl.style.color='red';")
      sb.Append("</script>")
 
      If (Not ClientScript.IsStartupScriptRegistered("JSScript")) Then
            ClientScript.RegisterStartupScript(Me.GetType(), "JSScript", sb.ToString())
      End If
End Sub

Revision: 34659
at October 26, 2010 23:36 by ramirocalero


Initial Code
C#
protected void Page_Load(object sender, EventArgs e)
{
    lblDisplayDate.Text = System.DateTime.Now.ToString("T");
}
 
protected void btnPostback_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");
 
    if (!ClientScript.IsStartupScriptRegistered("JSScript"))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
    }
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      lblDisplayDate.Text = System.DateTime.Now.ToString("T")
End Sub
 
Protected Sub btnPostback_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
      sb.Append("<script language='javascript'>")
      sb.Append("var lbl = document.getElementById('lblDisplayDate');")
      sb.Append("lbl.style.color='red';")
      sb.Append("</script>")
 
      If (Not ClientScript.IsStartupScriptRegistered("JSScript")) Then
            ClientScript.RegisterStartupScript(Me.GetType(), "JSScript", sb.ToString())
      End If
End Sub

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

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

Initial Title
Add javascript in VB.Net and C#

Initial Tags
javascript, script, Net, c#

Initial Language
VB.NET