/ Published in: VB.NET
Hay dos maneras, la mejor es usar el script manager, tambien ver la diferencia entre
http://forums.asp.net/t/902895.aspx
http://forums.asp.net/t/902895.aspx
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
URL: http://www.dotnetcurry.com/ShowArticle.aspx?ID=200&AspxAutoDetectCookieSupport=1