如何利用的SqlParameter ASP.NET当前用户名,而不code-背后 [英] How to utilize ASP.NET current user name in SqlParameter without code-behind
问题描述
我如何为当前用户名,而不在$ C $做C-背后,只是用ASPX服务器标签?
How do I get to the current users name without doing it in the code-behind, just using the aspx server tags?
在code-后面我可以做到这一点:
In code-behind I can just do this:
Label4.Text = User.Identity.Name.ToString()
但我试图做到这一点,而不code-背后是这样的:
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>
我得到线6,7所显示的用户名和9,但我真的想控件的属性设置为这个值,而不是仅仅显示在屏幕上。
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.
这可能吗?
背景:我是掀起了一个快速的应用程序,并拖动页面上的下降控制,以及原来,我只具有页面(S)的code-落后1线做到了。这条线是在页面加载的隐藏字段的值设置为当前用户的名字,所以我可以通过该控件的值作为一个的SqlParameter的参数。所以我想,既然我是走这条路(许多内容,ASPX,也许不应该存在)我应该尝试与它保持一致。我通常不这样来做,但希望这个时间
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
推荐答案
在你写了评论:
另外,如果我能得到任何控制
值设置为这个用户名我所能
然后指定我的SqlParameter是
controlparameter这将让我
我需要太。
Also, if I could get any controls value set to this username I could then specify my sqlparameter is a controlparameter and that would get me where I need to be too.
您可以创建自定义参数类型。
You can create a custom parameter type.
通过创建这个类开始,无论是在APP_ code或在ASP.NET服务器控件的dll:
Start by creating this class, either in App_Code or in a ASP.NET Server control dll:
namespace ParameterDemo {
public class LoginParameter : Parameter {
public LoginParameter(string name)
: base(name)
{}
protected override object Evaluate(HttpContext context, Control control)
{
//UPDATED as suggested in Joels comments below...
//return HttpContext.Current.User.Identity.Name;
return context.Current.User.Identity.Name;
}
}
}
和注册在页面上(@Page指令后右)
and registering it on the page (right after the @Page directive)
<%@ Register TagPrefix="put" Namespace="ParameterDemo" %>
(或可选其注册在web.config中所有网页上使用)
(or optionally register it in web.config for use on all pages)
...和你可以使用这样的:
...and the you can use it like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT * FROM MyTable WHERE SomeValue=@SomeParameter">
<SelectParameters>
<put:loginParameter name="SomeParameter" />
</SelectParameters>
</asp:ObjectDataSource>
如果这是你在找什么,你应该考虑修改原来的问题...
If this is what you're looking for, you should consider editing the original question...
这篇关于如何利用的SqlParameter ASP.NET当前用户名,而不code-背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!