Click to See Complete Forum and Search --> : how to direct response to another frame in c#


Farhad
December 11th, 2002, 12:22 AM
In my application there are two frames say A and B

There is a server side control in frame A

now when something happens on client side in frame A, I want to change the page in frame B through the C# class of page in frame A

How can i do that please tell me

Thanks in advance

dancolasanti
December 11th, 2002, 08:30 PM
In ASP.NET the server side code does not know about client side
document setup (i.e. frames). So in order to do this, you need to
add client side script to the postback to the client that will run at
the client.

To do this, I use these C# functions:

static public void RunClientJavaScript(Page page, string script) {
string scriptTag = "<script language='JavaScript'>" + script + "</script>";

if (!page.IsStartupScriptRegistered("redirect"))
page.RegisterStartupScript("redirect", scriptTag);
}

static public void FrameRedirect(Page page, string url, string target) {
RunClientJavaScript(
page, "parent.frames['" + target + "'].location.href='" + url + "';");
}

You could easily merge them into a single FrameRedirect
function if you want to, although I find the RunClientJavaScript
function useful for other things.

Put them in your class, or better yet in a global class or one that specificly is set up to handle client-specific issues like this.
Assuming that you put this code in a class called ClientScripter,
you could then call it from a another class (you would either have
to put ClientScripter in your project or build it to a .dll and add a
reference to it in your project).

For example, I have a login page that loads into my "docframe" frame, and a login-status that is on the "login_status_frame"
frame. The Page_Load function in my Login.aspx file, my
Forms Authentication function "On_Login(...)", and my
LoginStatus.aspx file's "SignOut()" function each has a line
that looks like this:

ClientScripter.FrameRedirect (
this,"LoginStatus.aspx","login_status_frame");

This refreshes the LoginStatus.aspx page in the
login_status_frame, which either says (A) "Please sign in" or
(B) "<username> is signed in" and a "sign out" button,
depending on whether a user is currently authenticated.

TheCPUWizard
December 30th, 2002, 10:30 AM
Nice implementation! :)

I was just wondering if you could provide some hints on generalizing this to include the following features:

1) Tell a frame to just refresh itself (i.e. Just re-load the current page)

2) Find out what page a frame is currently Displaying? (i.e. URL with current parameters)

3) Tell a frame to invoke its POST method. In this case I am dealing with a rather complex situation. Simplified it resolves down to:

a) Frame A has a textbox. When the user types in this text box, NO postback is to be performed.

b) Frame B has a button. When the user clicks the button, the server needs to tell Frame A to postback its current contents.

Assuming I know only the names of the frames and controls, can this be implemented in a totally server side solution (obviously client side script injection is required...).

=======================

Thanks in Advance.

TheCPUWizard
December 30th, 2002, 10:47 AM
Copied you sample snippets into my application, noticed that your RunClientJavaScript method does not use the passed in script parameter....

Curious how the script is suposed to actually execute... Especially since it does not work! :(

dancolasanti
December 30th, 2002, 11:28 AM
Hello,
Sorry, I just noticed that the codeguru site must have stripped out my "script" tags that were in the source code because I
didn't use the ampersand notation for the angled brackets:

The line that shows up as: string scriptTag = "";

should read as follows:

string scriptTag = "&lt;script language='JavaScript'&gt;" + script + "&lt;/script&gt;";

Let me know if there is still a problem...
Good luck,
Dan