Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am trying to get an asp:SessionParameter of a SelectParameters, to use a property of an object in session
instead of having a string in the session like Session["CustomerID"]

Something like Session["Customer"] where the property is ((Customer) Session["Customer"]).CustomerID)

I don’t know the syntax to do that, if you can help, I’d really appreciate it

Thank you

My code:

<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:DBConnectionString %>" xmlns:asp="#unknown">
        SelectCommand="SELECT * FROM getStoreCustomers (@customerID,@week,@day)"
        ProviderName="System.Data.SqlClient">
        <selectparameters>
            <asp:sessionparameter name="customerID" sessionfield="Customer" /> ?????? (what is the syntax to pass the property here?)
            <asp:controlparameter controlid="ddWeek" defaultvalue="-1" name="week" propertyname="SelectedValue" />
            <asp:controlparameter controlid="ddDay" defaultvalue="-1" name="day" propertyname="SelectedValue" />
        </selectparameters>
    </asp:sqldatasource>

The class I use is like any other class (but serializable)

[Serializable]
    public class Customer
    {
        public int CustomerID
        {
            get;
            set;
        }
    }
Posted
Updated 19-Jul-10 18:25pm
v2
Comments
PSK_ 20-Jul-10 1:21am    
I don't think that you can bind the property of a class stored in session, in sessionfield you can specify only a key stored in session.

1 solution

I just did resolve this issue by deriving my own class from SessionParameterInfo.

See this example as a start:

namespace MyNameSpace
{
	using System.Reflection;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;

	public class LoggedInUserSessionParameter :
		SessionParameter
	{
		protected override object Evaluate(
			HttpContext context,
			Control control)
		{
			if ((context != null) && (context.Session != null))
			{
				var fieldName = SessionField;

				var liui = context.Session[@"CurrentUserInfo"] as LoggedInUserInformation;
				if (liui == null)
				{
					return null;
				}
				else
				{
					return liui.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, liui, null);
				}
			}
			else
			{
				return base.Evaluate(context, control);
			}
		}
	}
}


You than can register the control on your page:

<%@ Register Namespace="MyNameSpace" TagPrefix="mns" %>


And then use it like this:

<mns:LoggedInUserSessionParameter Name="LegalEntityID" SessionField="LegalEntityID" Type="Int32" />


Regards
Uwe
 
Share this answer
 
Comments
Dalek Dave 23-Nov-10 4:40am    
Good answer, Uwe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900