ASP.NET LinqDataSource WHERE 子句
我需要为我的 LinqDataSource
创建一个 WHERE 子句,这取决于当前登录的用户。当然,我可以在代码隐藏中访问当前登录的用户,并且我特别需要用户 ID 来从数据库中仅获取属于他/她的数据。
当我将 Where
属性添加到
标记时,它工作正常,但由于服务器控件不能有 <% %>< /code> 标签中,我尝试在数据绑定连接到我的数据源的 GridView 之前在代码隐藏的
OnLoad
中设置 Where
属性。
但是,在代码隐藏中设置属性时,似乎没有效果。当我在 ascx
代码中手动(静态)指定它时,它工作得很好,但不是从代码隐藏中指定。
我猜我以错误的顺序或在错误的时间做事。我该怎么做?
更新:
想出了这个技巧。我使用虚拟标签 lblViewedUserID
和 Visible="false"
来获取可以从中提取用户 ID 的控件。我在数据绑定之前从代码隐藏中设置了该标签的文本。
我还添加了
以及带有一堆属性的
。
<asp:LinqDataSource ID="dsLinqSource" AutoPage="true" AutoSort="true"
ContextTypeName="Ortrac.Common.Dbml.OrComDataContext"
EnableDelete="false" TableName="myTableName" EnableInsert="false"
EnableUpdate="false" runat="server" Select="new(Edited, Activity)"
Where="TheUserID.ToString().Equals(@ViewedUserID)"><%-- HACK! --%>
<WhereParameters>
<asp:ControlParameter Name="ViewedUserID" ControlID="lblViewedUserID"
Type="String" PropertyName="Text" />
</WhereParameters>
</asp:LinqDataSource>
<%-- Dummy label --%>
<asp:Label runat="server" ID="lblViewedUserID" Visible="false" />
这真的是 ASP.NET 编程方式吗?
I need to make a WHERE clause for my LinqDataSource
which depends on the currently logged in user. Of course, I have access to the currently logged in user in the code-behind and I specifically need the user ID to fetch only data belonging to him/her from the database.
It works fine when I add the Where
attribute to the <asp:LinqDataSource />
tag but since server controls cannot have <% %>
tags in them, I tried to set the Where
attribute in the code-behind, in OnLoad
, before data-binding the GridView which is connected to my data source.
However, when setting the attribute in the code-behind, it seems to have no effect. It works fine when I specify it manually (and statically) in the ascx
code, but not from the code-behind.
I'm guessing I'm doing things in the wrong order or at the wrong times. How do I do this?
UPDATE:
Came up with this hack. I use a dummy label lblViewedUserID
with Visible="false"
to get a control from which I can extract the user ID. I set the text of this label from the code-behind before databinding.
I also added <WhereParameters>
with a <asp:ControlParameter />
with a bunch of attributes.
<asp:LinqDataSource ID="dsLinqSource" AutoPage="true" AutoSort="true"
ContextTypeName="Ortrac.Common.Dbml.OrComDataContext"
EnableDelete="false" TableName="myTableName" EnableInsert="false"
EnableUpdate="false" runat="server" Select="new(Edited, Activity)"
Where="TheUserID.ToString().Equals(@ViewedUserID)"><%-- HACK! --%>
<WhereParameters>
<asp:ControlParameter Name="ViewedUserID" ControlID="lblViewedUserID"
Type="String" PropertyName="Text" />
</WhereParameters>
</asp:LinqDataSource>
<%-- Dummy label --%>
<asp:Label runat="server" ID="lblViewedUserID" Visible="false" />
Is this really how you program ASP.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码实际上是一种完全可以接受的方法。如果您需要经常这样做,您可以继承 Parameter 类来创建您自己的自定义参数。这就是我们所使用的:
这在许多情况下都很有效 - 我们还有 CurrentLanguageIdParameter、CurrentInstanceIdParameter 等。
The code you posted is actually a perfectly acceptable approach. If you're going to need to do this often, you can inherit from the Parameter class to make your own custom parameters. This is what we use:
This works well in a number of situations - we also have a CurrentLanguageIdParameter, CurrentInstanceIdParameter, etc.