Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to utilize ASP.NET current user name in SqlParameter without code-behind

How do I get to the current users name without doing it in the code-behind, just using the aspx server tags?

In code-behind I can just do this:

Label4.Text = User.Identity.Name.ToString()

But I'm trying to do it without code-behind like this:

<body>
    <form id="form1" runat="server">
    <div>
        1. <asp:Label ID="Label1" runat="server" Text="<% User.Identity.Name %>"/><br />
        2. <asp:Label ID="Label2" runat="server" Text="<%= User.Identity.Name %>"/><br />
        3. <asp:Label ID="Label3" runat="server" Text="<%# User.Identity.Name %>"/><br />
        4. <asp:Label ID="Label4" runat="server" Text="<%= Context.User.Identity.Name %>"/><br />
        5. <asp:Label ID="Label5" runat="server" Text='<%# User.Identity.Name %>' /><br />
        6. <span runat="server" ID="Span1"><%= User.Identity.Name %></span><br />
        7. <asp:LoginName ID="LoginName1" runat="server" /><br />
        8. <span><%# User.Identity.Name %></span><br />
        9. <span><%= User.Identity.Name %></span><br />
        10. <asp:Label ID="Label6" runat="server" Text='<%= User.Identity.Name %>' /><br />
    </div>
    </form>
</body>

I get the username displayed for lines 6, 7, and 9 but I really want to set a property of a control to this value and not just display it on screen.

Is it possible?

Background: I was whipping up a quick app, dragging and dropping controls on the page, and it turned out that I did it with only having 1 line in code-behind of the page(s). That line was setting the value of a hidden field to the current users name in page load so I could pass the value of that control as a param of a sqlparameter. So I thought that since I was going this route (lots of stuff in aspx that maybe shouldn't be there) I should try to be consistent with it. I don't normally do it this way, but wanted to this time

like image 835
Keith Avatar asked Oct 15 '09 19:10

Keith


3 Answers

Will this do the trick?

<asp:LoginName ID="LoginName1" runat="server" />
like image 93
Arjan Einbu Avatar answered Oct 30 '22 23:10

Arjan Einbu


If you really want to pass the current user name as a select parameter to SqlDataSource, I'd suggest making a quick custom parameter (either as a code file in your web project or in a separate assembly if you like):

namespace CustomParameters
{
    public class UserNameParameter : Parameter
    {
        public UserNameParameter()
        {
        }

        public UserNameParameter(string name)
            : base(name)
        { }


        protected override object Evaluate(HttpContext context, Control control)
        {
            return User.Identity.Name;
        }
    }
}

and then in your page:

<%@ Register TagPrefix="myNS" Namespace="CustomParameters" %>

...

<myNS:UserNameParameter Name="UserName" />
like image 43
Peter Lillevold Avatar answered Oct 30 '22 23:10

Peter Lillevold


Do you really need it in a label server control? Or could you just use the span tags rendered by the server control?

<span runat="server" ID="Label2"><%= User.Identity.Name %></span>

Update:
Okay, new goal: Get User.Identity.Name into an SqlParameter value without using the codebehind.

This is going to be tricky. The basic code tag bee-stings (<% %> and the like) don't run in the page life cycle until after your query was already executed. That means you need to handle an earlier page life cycle event yourself, and the usually means putting something in the code behind. If you really want to get rid of the code-behind you can of course include a server-side script on your page:

<%@ Page Lanuage="C#" %>
<script runat="server" language="c#">
    public void OnSelecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
       e.Command.Parameters["@UserName"].Value = User.Identity.Name;
    }
</scirpt>
<html>
<body>
    <asp:SqlDataSource runat="server" ID="MyDataSource" OnSelecting="OnSelecting" ...>
    <SelectParameters>
        <asp:Parameter Name="UserName" ... />
    </SelectParameters>
    </asp:SqlDataSource>

    <asp:GridView runat="server" ID="ResultsGrid" DataSourceID="MyDataSource" .../>

</body>
</html>

It's still writing real code, though, rather than keeping everything in markup. But I suspect it's the closest you can get here.

like image 2
Joel Coehoorn Avatar answered Oct 30 '22 23:10

Joel Coehoorn