如何在 asp:SessionParameter 中使用对象的属性

发布于 2024-09-11 05:02:19 字数 1140 浏览 5 评论 0原文

我正在尝试获取 SelectParametersasp:SessionParameter,以在会话中使用对象的属性 而不是在会话中使用诸如 Session["CustomerID"] 之类的字符串,

类似于 Session["Customer"] ,其中属性为 ((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;
}
} 

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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橙幽之幻 2024-09-18 05:02:20

从彼得那里找到了方法。

的 SqlDataSource 的 Selectingevent 中设置值:

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

“改用常规参数,并在事件处理程序中

    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();           
        }

}

Found how to, from peter.

"Use a regular parameter instead, and set the value in the Selectingevent of the SqlDataSource

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

in the event handler:

    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();           
        }

}
雨落□心尘 2024-09-18 05:02:20

我明白你在说什么,但我认为该参数不够智能,无法获取会话中存储的对象的属性。我认为它必须是原始类型,否则使用并通过代码显式设置 DefaultValue 属性:

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

I understand what you are saying, but I don't think the parameter is intelligent enough to get a property of the object stored in session. I think it has to be a primitive type, or else use an and explicitly set the DefaultValue property via code:

ds.SelectParameters["ParamName"].DefaultValue = ((Customer)Session["Customer"]).CustomerID;
愁杀 2024-09-18 05:02:20

您需要处理 sqldatasource 的 OnSelecting 事件并在其中添加参数。

You'll need to handle the OnSelecting event of your sqldatasource and add the parameter there.

飞烟轻若梦 2024-09-18 05:02:20

我编写了以下类来让它为我工作:

/// <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]);
    }
}

有了这个,我可以将它用作 SqlDataSource 上的简单参数:

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

I wrote following class to get this working for me:

/// <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]);
    }
}

With this I can use it as a simple Parameter on the SqlDataSource:

    <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文