Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 514821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:33:52+00:00 2026-05-13T07:33:52+00:00

I need to make a WHERE clause for my LinqDataSource which depends on the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:33:53+00:00Added an answer on May 13, 2026 at 7:33 am

    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:

    public class CurrentUserIdParameter : System.Web.UI.WebControls.Parameter
    {
    
        protected override int Evaluate(System.Web.HttpContext context, System.Web.UI.Control control)
        {
            if (Contact.Current == null) {
                return -1;
            }
            else {
                return Contact.Current.ContactID;
            }
        }
    }
    

    This works well in a number of situations – we also have a CurrentLanguageIdParameter, CurrentInstanceIdParameter, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have to define the Read, Write, Modify, etc. columns… May 13, 2026 at 7:35 am
  • Editorial Team
    Editorial Team added an answer In the case of the example URL http://en.example.com/file.php, the host… May 13, 2026 at 7:35 am
  • Editorial Team
    Editorial Team added an answer Well actually I figured it out myself in the end,… May 13, 2026 at 7:35 am

Related Questions

I’m using C# and Winforms. I’m trying to make a report that gives a
I've got a scenario where I need to do a join across three tables.
After this question , I need to know what principles should be followed in
I'm using nested sets (aka modified preorder tree traversal) to store a list of
This is a real newbie question, but I am struggling to get this to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.