如何在 asp:SessionParameter 中使用对象的属性
我正在尝试获取 SelectParameters
的 asp: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从彼得那里找到了方法。
的 SqlDataSource 的 Selectingevent 中设置值:
“改用常规参数,并在事件处理程序中
Found how to, from peter.
"Use a regular parameter instead, and set the value in the Selectingevent of the SqlDataSource
in the event handler:
我明白你在说什么,但我认为该参数不够智能,无法获取会话中存储的对象的属性。我认为它必须是原始类型,否则使用并通过代码显式设置 DefaultValue 属性:
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:
您需要处理 sqldatasource 的 OnSelecting 事件并在其中添加参数。
You'll need to handle the OnSelecting event of your sqldatasource and add the parameter there.
我编写了以下类来让它为我工作:
有了这个,我可以将它用作 SqlDataSource 上的简单参数:
I wrote following class to get this working for me:
With this I can use it as a simple Parameter on the SqlDataSource: