2010-07-20 2 views
1

내가 Session["CustomerID"]ASP에서 객체의 속성 사용 방법 : SessionParameter

같은 세션에서 문자열을하는 대신 세션 에서 개체의 속성을 사용하는 SelectParametersasp:SessionParameter를 얻기 위해 노력하고 있어요을 재산은 Session["Customer"] 같은 뭔가 ((고객) Session["Customer"]).CustomerID)

나는 구문 당신이 도울 수 있다면, 나는 정말

당신에게

감사 감사하겠습니다 것을 해야할지 모르겠어요

내 코드 :

<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> 

내가 사용하는 클래스는 다른 클래스 (그러나 직렬화) 나는 당신이 무슨 말을하는지 이해

[Serializable] 
public class Customer 
{ 
public int CustomerID 
{ 
get; 
set; 
} 
} 

답변

1

처럼,하지만 난 매개 변수가 지능형 생각하지 않습니다 세션에 저장된 객체의 속성을 가져올 정도로 충분합니다.

ds.SelectParameters["ParamName"].DefaultValue = ((Customer)Session["Customer"]).CustomerID; 
0

당신은 당신의 SqlDataSource의 OnSelecting 이벤트를 처리하고이 매개 변수를 추가해야합니다 : 나는 코드를 통해 DefaultValue 속성을을 사용하여 명시 적으로 설정 다른 원시적 형, 또는 할 수있다 생각합니다.

2

발견 방법, 피터에서. 이 나를 위해 작업을 얻을 수

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 
     if (Session["Customer"] != null) 
     { 
      Customer c = (Customer)Session["Customer"]; 
      e.Command.Parameters[0].Value = c.CustomerID.ToString();   
     } 

} 
0

내가 클래스를 다음 쓴 :

는 "대신 일반 매개 변수를 사용하고 이벤트 처리기에서의 SqlDataSource

<asp:Parameter Name="customerID" Type="String" /> 

의 Selectingevent에 값을 설정 :

/// <summary> 
/// Allows to access field-values of a object in Session. 
/// </summary> 
public class SessionObjectParameter : Parameter 
{ 
    public string SessionField { get; set; } 
    public string PropertyName { get; set; } 

    protected override object Evaluate(HttpContext context, System.Web.UI.Control control) 
    { 
     if (string.IsNullOrEmpty(SessionField)) 
      throw new Exception("SessionField must be definied"); 

     if (string.IsNullOrEmpty(PropertyName)) 
      throw new Exception("PropertyName must be definied"); 

     PropertyInfo pi = context.Session[SessionField].GetType().GetProperty(this.PropertyName); 
     if (pi == null) 
      throw new Exception(string.Format("PropertyName '{0}' is not definied in the object at Session['{1}'].", this.PropertyName, this.SessionField)); 

     return pi.GetValue(context.Session[SessionField]); 
    } 
} 

나는 이것을 SqlData의 간단한 매개 변수로 사용할 수 있습니다. 출처 :

<asp:SqlDataSource ID="dsTest" runat="server" ConnectionString='<%$ ConnectionStrings:con %>' SelectCommand="SELECT * FROM Test WHERE ([Field] = @ParValue)"> 
     <SelectParameters> 
      <costum:SessionObjectParameter Name="ParValue" SessionField="MySessionObject" PropertyName="Value" /> 
    </asp:SqlDataSource> 
관련 문제