Passing data

Discussion in 'ASP .Net' started by Ludwig, Apr 9, 2007.

  1. Ludwig

    Ludwig Guest

    I have a user control news.ascx that lists news items, and it has
    add/edit/delete linkbuttons.

    This user control is on the default.aspx page together with other user
    controls.

    When the edit linkbutton for a news item is clicked, it redirects to
    another page news.aspx, the news user control is also on this page.

    Question: when the user clicks the edit linkbutton in the user control
    on the default.aspx page, the data of the corresponding news item
    should be passed to the control on the news.aspx page. What is the
    best way to do that?

    I was thinking of using the Session to store this data; but maybe
    there's a better way?

    Thanks for the suggestions!

    --
    Ludwig
    http://www.coders-lab.be
     
    Ludwig, Apr 9, 2007
    #1
    1. Advertising

  2. http://msdn2.microsoft.com/en-us/library/z1hkazw7(vs.71).aspx





    "Ludwig" <ludwig.stuyck(remove)@telenet.be> wrote in message news:p...
    >I have a user control news.ascx that lists news items, and it has
    > add/edit/delete linkbuttons.
    >
    > This user control is on the default.aspx page together with other user
    > controls.
    >
    > When the edit linkbutton for a news item is clicked, it redirects to
    > another page news.aspx, the news user control is also on this page.
    >
    > Question: when the user clicks the edit linkbutton in the user control
    > on the default.aspx page, the data of the corresponding news item
    > should be passed to the control on the news.aspx page. What is the
    > best way to do that?
    >
    > I was thinking of using the Session to store this data; but maybe
    > there's a better way?
    >
    > Thanks for the suggestions!
    >
    > --
    > Ludwig
    > http://www.coders-lab.be
     
    Jon Paal [MSMD], Apr 9, 2007
    #2
    1. Advertising

  3. Ludwig

    sloan Guest

    When I have a need like this, I use a custom " data store keeper " to
    persist the info.
    Before I go into that, think of it as just a fancy Session object wrapper.

    I also create a specific (serializable) object used for this. Instead of
    putting 3 (seperate) strings and ints in the Session, I create a mini object
    specifically for passing info from default.aspx to news.aspx.

    public class NewsEditArgs
    --int NewsItemID
    --string Something Else

    Then I put this in the Server memory, ~with a specific key.

    As soon as the other page (news.aspx) runs, I pull it out (remove it) and
    use it on the news.aspx page.

    This is so I don't accidently orphan records there.

    I wrote one of these:
    10/24/2005
    Web Session Wrapper for storing and retrieving objects
    http://sholliday.spaces.live.com/blog/


    This is so I have a very good .Add and .Remove method (to avoid orphans).

    However, if this is a "one page away" scenario, then look here
    http://msdn2.microsoft.com/en-us/library/aa713401(VS.71).aspx
    and at the other mentioned MS article.






    "Ludwig" <ludwig.stuyck(remove)@telenet.be> wrote in message
    news:p...
    > I have a user control news.ascx that lists news items, and it has
    > add/edit/delete linkbuttons.
    >
    > This user control is on the default.aspx page together with other user
    > controls.
    >
    > When the edit linkbutton for a news item is clicked, it redirects to
    > another page news.aspx, the news user control is also on this page.
    >
    > Question: when the user clicks the edit linkbutton in the user control
    > on the default.aspx page, the data of the corresponding news item
    > should be passed to the control on the news.aspx page. What is the
    > best way to do that?
    >
    > I was thinking of using the Session to store this data; but maybe
    > there's a better way?
    >
    > Thanks for the suggestions!
    >
    > --
    > Ludwig
    > http://www.coders-lab.be
     
    sloan, Apr 9, 2007
    #3
  4. Ludwig

    Ludwig Guest

    On Mon, 9 Apr 2007 14:18:00 -0400, "sloan" <> wrote:

    >
    >
    >When I have a need like this, I use a custom " data store keeper " to
    >persist the info.
    >Before I go into that, think of it as just a fancy Session object wrapper.
    >
    >I also create a specific (serializable) object used for this. Instead of
    >putting 3 (seperate) strings and ints in the Session, I create a mini object
    >specifically for passing info from default.aspx to news.aspx.
    >
    >public class NewsEditArgs
    > --int NewsItemID
    > --string Something Else
    >
    >Then I put this in the Server memory, ~with a specific key.
    >
    >As soon as the other page (news.aspx) runs, I pull it out (remove it) and
    >use it on the news.aspx page.
    >
    >This is so I don't accidently orphan records there.
    >
    >I wrote one of these:
    >10/24/2005
    >Web Session Wrapper for storing and retrieving objects
    >http://sholliday.spaces.live.com/blog/
    >
    >
    >This is so I have a very good .Add and .Remove method (to avoid orphans).
    >
    >However, if this is a "one page away" scenario, then look here
    >http://msdn2.microsoft.com/en-us/library/aa713401(VS.71).aspx
    >and at the other mentioned MS article.
    >


    Thank you! I'll look into it! PS - what do you mean with orphan
    records?

    --
    Ludwig
    http://www.coders-lab.be
     
    Ludwig, Apr 10, 2007
    #4
  5. Ludwig

    Ludwig Guest

    On Mon, 9 Apr 2007 14:18:00 -0400, "sloan" <> wrote:

    >
    >
    >When I have a need like this, I use a custom " data store keeper " to
    >persist the info.
    >Before I go into that, think of it as just a fancy Session object wrapper.
    >
    >I also create a specific (serializable) object used for this. Instead of
    >putting 3 (seperate) strings and ints in the Session, I create a mini object
    >specifically for passing info from default.aspx to news.aspx.
    >
    >public class NewsEditArgs
    > --int NewsItemID
    > --string Something Else
    >
    >Then I put this in the Server memory, ~with a specific key.
    >
    >As soon as the other page (news.aspx) runs, I pull it out (remove it) and
    >use it on the news.aspx page.
    >
    >This is so I don't accidently orphan records there.
    >
    >I wrote one of these:
    >10/24/2005
    >Web Session Wrapper for storing and retrieving objects
    >http://sholliday.spaces.live.com/blog/
    >
    >
    >This is so I have a very good .Add and .Remove method (to avoid orphans).
    >
    >However, if this is a "one page away" scenario, then look here
    >http://msdn2.microsoft.com/en-us/library/aa713401(VS.71).aspx
    >and at the other mentioned MS article.
    >


    Remark concerning your code: the instance in the datastore is a static
    variable, so this means that it stays in memory on application level,
    not session.... so it will be even if the session has ended. Is that
    intended?

    --
    Ludwig
    http://www.coders-lab.be
     
    Ludwig, Apr 10, 2007
    #5
  6. Ludwig

    sloan Guest

    By orphan, I mean....
    I put a value into the Session object. I use it. But it just "hangs out
    there" because I didn't remove it.

    Like I go to news.aspx, and read the object out of the Session... and
    populate the values in news.aspx.
    Then I go to a totally different place in my web app, never needing that
    item I put in .Session again.
    It just hangs out, taking up unnecessary resources.


    The static thing. This was just the way you do a singleton in asp.net.

    http://www.google.com/search?hl=en&q="asp.net" singleton

    Hmmm. You bring up an interesting point.
    But even though its a static method, way down deep in the .GetInstance is

    m_singletonInstance =
    (WebSessionDataStore)System.Web.HttpContext.Current.Session[SESSION_OBJECT_G
    UID];

    So that part is Session/User Specific.

    I do know I've had great success with it, in a multi user environment.

    PS

    Hints:
    On a "login" , I usually clear the cache...........and also on a "logout".
    What I mean is that when a user is verified, I go ahead an just make sure I
    do a .Clear() , just in case the SessionID got stuck.

    Sometimes the SessionID doesn't reset, even when you call Session.Abandon.

    If the user clicks a logout button, I just call the .Clear() to make sure
    there isn't something in Session memory orphaned.

    That's kinda why I really like the Wrapper, I have control of when I need an
    item, when I need to .Remove it, and when I need to wipe out everything.
    Without a bunch of riddled Session object code throughout.

    ids.Clear() is the method to call.


    Below is my new 2.0 version. With a little extra check about the SessionID

    You'll notice I have a IDataStore.
    I also have a "WeakReference" implementation of this Interface.
    You use a WeakReference, when you kinda want to cache stuff, but if there is
    a big load on the server, the object gets released.
    Aka, if the server is under load, and I cached up a list of departments or
    something, the WeakReference will say "this isn't that important, release
    it".
    But the code below means "I need that object to be there no matter what".
    I have a SimpleFactory that I throw a enum at, to get the 2 different
    versions.


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace GranadaCoder.CachingFramework
    {

    [Serializable]
    public class WebSessionDataStore //: IDataStore
    {

    //Guid ensures that uniqueid that won't be duplicated by accident
    private const string SESSION_OBJECT_GUID =
    "ABCABCAB-3754-4074-A007-051F9B64CA9E";

    private static WebSessionDataStore _singletonInstance ;//= null;
    private Dictionary<string, object> _memoryStore ;//= null;

    private static string _sessionId = string.Empty;

    #region Singleton Area

    private WebSessionDataStore()
    {
    this._memoryStore = new Dictionary<string, object>();
    }

    /// <summary>
    /// Singleton representing WebSessionDataStore.
    /// </summary>
    /// <returns></returns>
    public static WebSessionDataStore GetInstance()
    {
    if (null != System.Web.HttpContext.Current)
    {
    if (null != System.Web.HttpContext.Current.Session)
    {

    bool resetSession = false;


    if (System.Web.HttpContext.Current.Session.IsNewSession)
    {
    resetSession = true;
    }

    if
    (System.Web.HttpContext.Current.Session.SessionID.ToUpper() !=
    _sessionId.ToUpper())
    {
    resetSession = true;
    }


    if (resetSession)
    {
    _singletonInstance = null; //Its a new Session, to
    blow away the singleton, so a new one is created.
    }
    else
    {
    if (null !=
    System.Web.HttpContext.Current.Session[SESSION_OBJECT_GUID])
    {
    _singletonInstance =
    (WebSessionDataStore)System.Web.HttpContext.Current.Session[SESSION_OBJECT_G
    UID];
    }

    }


    }
    }

    if (null == _singletonInstance)
    {
    _singletonInstance = new WebSessionDataStore();
    System.Web.HttpContext.Current.Session[SESSION_OBJECT_GUID]
    = _singletonInstance;
    _sessionId =
    System.Web.HttpContext.Current.Session.SessionID;//set the sessionId//



    }
    return _singletonInstance;

    }

    #endregion


    #region IDataStore Members

    /// <summary>
    /// Removes all objects in the DataStore.
    /// </summary>
    public void Clear()
    {
    _memoryStore.Clear();
    }

    /// <summary>
    /// Adds an object with the specified key.
    /// </summary>
    /// <param name="key">The key.</param>
    /// <param name="value">The object to be added.</param>
    public void Add(string key, object value)
    {
    if (_memoryStore.ContainsKey(key))
    {
    _memoryStore.Remove(key);
    }

    _memoryStore.Add(key, value);
    }


    //The next method doesn't do anything
    ////// public void AddNotRemovable(string key, object value)
    ////// {
    ////// this.Add( key, value );
    ////// }

    /// <summary>
    /// Removes the object using the specified key.
    /// </summary>
    /// <param name="key">The key used originally used to Add the item
    to the DataStore.</param>
    /// <returns></returns>
    public object Remove(string key)
    {
    // see
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#remove(java.lang.Object)
    // for java method, which returns the object you removed, in
    case
    // you want to do something with it

    object returnObject = null;
    if (null != this._memoryStore)
    {
    if (_memoryStore.ContainsKey(key))
    {
    returnObject = this._memoryStore[key];
    _memoryStore.Remove(key);
    }

    }
    return returnObject;
    }

    /// <summary>
    /// Gets the <see cref="Object"/> with the specified key.
    /// </summary>
    /// <value></value>
    public object this[string key]
    {
    get
    {
    if(this._memoryStore.ContainsKey(key))
    {
    if (null != _memoryStore[key])
    {
    return _memoryStore[key];
    }
    }
    return null;
    }
    }

    /// <summary>
    /// Gets the number of items in the DataStore.
    /// </summary>
    /// <value>The size.</value>
    public int Size
    {
    get
    {
    if (null != _memoryStore)
    {
    return _memoryStore.Count;
    }
    return 0;
    }
    }

    #endregion
    }
    }
     
    sloan, Apr 11, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. terry
    Replies:
    1
    Views:
    483
    William F. Robertson, Jr.
    Jul 7, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    913
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    442
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    491
    Pascal J. Bourguignon
    Sep 13, 2009
  5. deadpickle
    Replies:
    1
    Views:
    1,029
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page