Wednesday, March 28, 2012

Calling a JS function from server side

Hi,

I try to call a javascript function from the server side but it doesn't work, here my code :

1<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">45<html xmlns="http://www.w3.org/1999/xhtml" >6<head runat="server">7 <title>Test</title>8</head>9<body>10 <form id="form1" runat="server">1112 <atlas:ScriptManager ID="scriptManager1" runat="server" EnablePartialRendering="true"></atlas:ScriptManager>1314 <atlas:UpdatePanel ID="UpdatePanel1" runat="server">15 <ContentTemplate>1617 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />18 <asp:TextBox ID="TextBox2" runat="server" Text="test"></asp:TextBox><br />19 <asp:Button ID="Button1" runat="server" Text="Write" OnClick="Button1_Click" />20 <asp:Button ID="Button2" runat="server" Text="Clear" OnClick="Button2_Click" />2122 </ContentTemplate>23 <Triggers>24 <atlas:ControlEventTrigger ControlID="Button1" EventName="Click" />25 <atlas:ControlEventTrigger ControlID="Button2" EventName="Click" />26 <atlas:ControlValueTrigger ControlID="TextBox1" PropertyName="Text" />27 </Triggers>28 </atlas:UpdatePanel>2930 </form>31</body>32</html>33

And the code behind :

1using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1112public partialclass test : System.Web.UI.Page13{14protected void Page_Load(object sender, EventArgs e)15 {1617 }1819public void Button1_Click(object sender, EventArgs e)20 {21 TextBox1.Text ="Button clicked";22 }2324public void Button2_Click(object sender, EventArgs e)25 {26 TextBox1.Text ="";27 Response.Write("<script type=\"text/javascript\">alert('hello');</script>");28 }2930}31

When i click on the button 2, my alert function is not called and the textbox 1 is not cleaned.

How can i call the JS function ?

Take a look at this post:http://forums.asp.net/thread/1395511.aspx
thank you :)

copy this code atButton2_Click

public void Button2_Click(object sender, EventArgs e)
25 {
26 TextBox1.Text ="";
27 Page.ClientScript.RegisterStartupScript(typeof(Page), "OnLoad", "alert('hello');",true );
28 }

See more details athttp://forums.asp.net/thread/1403704.aspx

No comments:

Post a Comment