Problem with alert message


V

Vivian

I've created a web page with ASP .NET and C#. Say my aspx filename is
"WebForm1.aspx", I have created another C# file named "Details.cs".

My problem is I want to prompt an alert message from both WebForm1.cs and
Details.cs, WebForm1.cs works fine, but no message can be shown from
Details.cs. The two files are in the same namespace with different class.
I've referred to WebForm1.cs.

The following is my code :

Namespace = "Test"

WebForm1.cs
===========
inside class WebForm1

public void PrintMsg(string key, string msg)
{
this.Page.RegisterClientScriptBlock(key, "<script
type='text/javascript'>alert('" + msg + "');</script>");
}

Details.cs
=======
inside class detailscode

private void PrintMessage(string key, string msg)
{
Test.WebForm1 oForm = new Test.WebForm1();
oForm.PrintMsg(key, msg);
}
 
Ad

Advertisements

I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

How you instantiate Details?

remember that the client is the browser, so only the page being executed is
the one that is generating the output.

You could change the code like this:

public void PrintMsg(string key, string msg, Page outputPage)
{
outputPage.RegisterClientScriptBlock(key, "<script
type='text/javascript'>alert('" + msg + "');</script>");
}

You would have to pass a reference to the current page (this) to the
instance of detailscode

Another possibility is using HttpContext. you would have access to the
Response object but you cannot use RegisterClientScriptBlock , you would
have to use Response.Write


In anyway I think you are confused about how a web app works. maybe if you
post more details about what you want to do and your current structure we
can help you further.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top