Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

OOD question

  Asked By: Darcy    Date: Feb 20    Category: Asp.net    Views: 1969

I have a basic object orientation design question. Is it possible to have
communication between 2 objects of different classes in C#?



26 Answers Found

Answer #1    Answered By: Hooriya Khan     Answered On: Feb 20

Also please bear this in mind that the 2 classes  have no relation in via inheritance.

Answer #2    Answered By: Adalia Fischer     Answered On: Feb 20

The .net Framework is written in C# and all the Framework objects  talk to each other. How specifically do you want them to communicate?

Answer #3    Answered By: Tracy Myers     Answered On: Feb 20

Infact I have 2 different pages. Now when say page 1 loads I create an object  of say Class X in the code behind. If the user clicks a hyperlink on Page1 then the Page 2 loads and the Class Y constructor (in the code behind of Page 2) is called. I wish I could pass the Class X object or its reference (not sure if we can do that in managed C#) in the Class Y constructor so that I could still use the Class X object. Do you think we can do this? If yes, then how to overload the call.

Otherwise can I use the Class X object in Class Y. I can't see it the browser object perhaps because it goes out of scope when page 1 unloads.

Answer #4    Answered By: Vonda Ramirez     Answered On: Feb 20

Just use a session to pass the object.

Discard the session var at end of second receiving page.

Answer #5    Answered By: Bach-yen Nguyen     Answered On: Feb 20

I will surely try that. But isn't there any better way of doing
it by the principles of OO.

Answer #6    Answered By: Lurline Fischer     Answered On: Feb 20

A session is an object  designed to transfer objects  between stateless HTTP requests. Winforms apps or console apps are NOT stateless so it is done naturally there.

HTTP is stateless as a protocol. As long as it is, the ASP.net team can never provide a way better than this.

Answer #7    Answered By: Alfonsine Miller     Answered On: Feb 20

You can make your second class persistable, either in a session or in the database.

Answer #8    Answered By: Fedde Bakker     Answered On: Feb 20

I have been trying to implement what you had said. Perhaps read somewhere that should avoid storing objects  in Sessions. But even having lots of problems

I have enabled sessions in both the ASP.NET pages as below:
<%@ Page language="c#" Codebehind="Categories.aspx.cs" AutoEventWireup="false" Inherits="produits_new.Categories" smartNavigation="False" enableSessionState="True" enableViewStateMac="True"%>
And I am able to store the object  in the Session in a xxx.cs file and access it later in another yyy.cs file but when the user clicks a hyperlink to open up another page I can not retrieve the session object in its code behind class. I added a watch on the session object, it says "an Exception of type System.Web.HttpException occured".

However the final error I get is "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive".

Do you have any idea as to why this is happening? I am already having a tough time ....:)

Answer #9    Answered By: Taylor White     Answered On: Feb 20

Delete EnableSessionState attribute in the @Page directive and make sure it's enabled in web.config, so Session will work in all pages. Then make sure that the new window you open is opened with window.open javascript function and not via A tag like: <a href="whatever_page.aspx" TARGET="_blank"> this way the other window will reside in a different session and it won't work. At least that's the way I think it is.

And of course, make sure both pages are part of the same application. Applications don't share Session object  collection...

Answer #10    Answered By: Cay Nguyen     Answered On: Feb 20

You are right. I replaced href using javascript function window.open and I could get back the session objects. But now without the hrefs I cannot display the links as its a menu page. Do you have any workarounds for this?

Answer #11    Answered By: Corbin Jones     Answered On: Feb 20

I don't think I get your problem now? Where do the hrefs get displayed? On window status bar. no problem. make an onmouseover event for A tag and set it as:
<A HREF="javascript:window.open(...);" onMouseOver="window.status='real link name';">

but you could also make it like this:

<A HREF="real_page.aspx" onClick="javascript: window.open(...); window.event.returnValue=false;">

this way the real link gets displayed in the status bar, but you handle clicks with the OnClick mouse event...

If you display link information in any other place, just let me know.

Answer #12    Answered By: Taylor Evans     Answered On: Feb 20

As I was not able to access the Session object  and you told me thats bcoz of the hrefs which open the window in a different session, I have replaced all the hrefs with winow.open - javascript function. It means :

I replaced:


After that I was able to access my session object. But now my problem is that the "Continents" word on the page doesn't appear as a link.. Is there any work around for this so as to get "Continents" link and keep the same session.

Answer #13    Answered By: Benjamin Simpson     Answered On: Feb 20

You could probably use a LinkButton and add the onclick to the attributes.

Answer #14    Answered By: Adalwen Fischer     Answered On: Feb 20

function openwin(myurl)
<a href="page.aspx" target=new>Page</a>
change to
<a href="javascript:void(0)" onclick=openwin("page.aspx")>Page</a>

Answer #15    Answered By: Dylan Evans     Answered On: Feb 20

This is much easier and more robust if you
use LinkButtons, as Rion suggested.
You can add an attribute to the LinkButton
in your Page_Load or some other initialization

LinkButton.Attributes.Add("onclick", "javascript:window.open('" + "continents.aspx" + "'," +"'linkwindow', 'width=405,height=465')");

and this enables you to use different window sizes, if you choose, or
even add parameters to the QueryString that have come from
a database or other source.

Answer #16    Answered By: Kerry Wright     Answered On: Feb 20

Well if you would read carefully, you'd see I included onClick event to the A tag, AS WELL as the HREF attribute. This way, you keep the link and make your own click "adventure"... I'm copiing the part of the code again:

onClick="javascript: window.open('other_info/continents.aspx'); window.event.returnValue=false;">

Answer #17    Answered By: Miriam Green     Answered On: Feb 20

because that would cause the server event to fire instead of the client

Answer #18    Answered By: Alberta Miller     Answered On: Feb 20

not if you get rid of the eventhandler !

//this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);

Answer #19    Answered By: Debbie Reyes     Answered On: Feb 20

woops - my mistake - course it postsback

soz ... I'll think before scribbling next time.

Answer #20    Answered By: Leroy Schmidt     Answered On: Feb 20

I have done exactly as you have said. But the very inclusion of HREF tag, is creating problems as I cann't access my session object.

Answer #21    Answered By: Edwin Chavez     Answered On: Feb 20

ok. But I tend to be "economical" don't waste resources just because I can...
It's better to use A tag, because it's much more uncomplicated object  ober
LinkButton... Anyway. YOu could use either. You could also add onclick attribute
to the linkbutton as someone suggested earlier.

There are many ways to reach your goal, you choolse the one that most suits

Answer #22    Answered By: Burk Martin     Answered On: Feb 20

Ok. Now I think it's time for you to explain where and what you meant with "link is not displayed"? Is that the problem of the status bar or what. To find a suitable solution to your problem... I use this exact functionality on a project I+m currently working on. First window opens and sets a Session object  and opens a child window that checks for that session. Had no problems with it...

Answer #23    Answered By: Hubert Taylor     Answered On: Feb 20

Unfortunately, I am trying to do exactly the same as what you have been doing but I lose my session object. There is nothing happening at the status bar. But I was just going through what Stephen had suggested. It works great !!!




Either of these do the job without losing the session object. Many many thanks to Stephen and you and other members as well for being so patient and helpful.

Answer #24    Answered By: Lurlene Fischer     Answered On: Feb 20

Actually I prefer Roberts version with trhe status bar set to the name of the file.

Anyone ... I am absolutely positive but cannot for the life of me recreate it .... I have it in a book (somewhere) .... how to change the class of something on rollover.

That way you could use a <asp:hyperlink> with url set to nothing, onclick as below, and mimick the A rollover

HyperLink hl = new HyperLink()
hl.Attributes["onclick"] = blah blah

hl.Attributes["onmouseover"] = "this.class='rollover'";
hl.Attributes["onmouseout"] = "this.class='rollunder'";

But I cannot get it to work ... maybe using "style=...." .. I dunno ... my book is at the bottom of a box with a load of junk on top ( am doing the kitchen up you see).

Any good to anyone ?

Answer #25    Answered By: Helene Stewart     Answered On: Feb 20

Actually the class attribute is named in javascript as classNamenot just class. But don't even bother doing that. Just make a class for normal A and a class with :hover for mouse over event. It works much faster than interpreted javascript. I use it all the time.

Answer #26    Answered By: Feodora Bonkob     Answered On: Feb 20

I keep promising myself to remember that every time I hit the problem, but I always forget.


Didn't find what you were looking for? Find more on OOD question Or get search suggestion and latest updates.