How to trigger client script from server script?

  • Thread starter Thread starter Manu
  • Start date Start date
M

Manu

In my ASPX code I would like to open a new window, send a message box,
create an alert window etc. The logic when to do the above is
implemented in the server side, as it is depended upon Database
information. (i.e. open an alert window when a record was not found in
the database.)
As I know opening such windows can be done on the client side only.
How can I trigger these scripts from the SERVER side?
Is there an example explaining this?
 
Hi Manu,

Well you have several options that are all based on the same idea, you have
a server side control , a Literal to me more precise , where you pass the
code or set the variables needed at the client, the value of this Text
property goes unchanged to the client, therefore you can do something like:
literal.Text= "<script> Alert('Hello');</script>";
this.Controls.Add( literal);

Now this is the basic idea, from there you have to code what you need, you
can set a function in the OnLoad event of the Body that call a function ,
and you use the literal to set a variable to decide what to do, etc

Another idea without the use of the Literal control may be setting the value
of OnLoad to one of several functions already defined in the HTML page, this
is useful if you have a predefined number of messages that do not change
( like a not found message ) , for this you have to mark the Body with
runat="server":
<body ID="Body" runat="server">

then in the server side you declare it like:
protected HtmlGenericControl Body;

and then add the correct function to the OnLoad using
Body.Attributes.Add("OnLoad", "FunctionToCall");


Hope this help,
 
Send the javascript to the client via Response.Write...

string msg = "<script language=\"javascript\">";
msg += "alert('Your message here');";
msg += "</script>";
Response.Write( msg );
 
Back
Top