如何在代码隐藏中使用aspx服务器标签来获取当前用户名?
在代码隐藏中,我可以这样做:
Label4.Text = User.Identity.Name.ToString()
但是我试图在没有代码隐藏的情况下这样做:
<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行显示的用户名,但我真的想将控件的属性设置为此值,而不是仅在屏幕上显示它。
有可能吗?
背景:我正在掀起一个快速的应用程序,在页面上拖放控件,结果发现我只在页面代码隐藏处有一行代码。该行是在页面加载中将隐藏字段的值设置为当前用户名,因此我可以将该控件的值作为sql参数的参数传递。所以我认为,因为我走这条路(很多东西在aspx中可能不应该存在)我应该尝试与它保持一致。我通常不会这样做,但是这次想要
答案 0 :(得分:8)
在你写的评论中写道:
另外,如果我能得到任何控制 值设置为此用户名我可以 然后指定我的sqlparameter是一个 控制参数,那会得到我 我需要去哪里。
您可以创建自定义参数类型。
首先在App_Code或ASP.NET Server控件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指令之后)
<%@ Register TagPrefix="put" Namespace="ParameterDemo" %>
(或者可选择在web.config中注册以在所有页面上使用)
...你可以像这样使用它:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT * FROM MyTable WHERE SomeValue=@SomeParameter">
<SelectParameters>
<put:loginParameter name="SomeParameter" />
</SelectParameters>
</asp:ObjectDataSource>
如果您正在寻找,请考虑编辑原始问题......
答案 1 :(得分:4)
如果你真的想将当前用户名作为select参数传递给SqlDataSource,我建议你制作一个快速的自定义参数(如果你愿意,可以作为web项目中的代码文件或单独的程序集): / p>
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;
}
}
}
然后在你的页面中:
<%@ Register TagPrefix="myNS" Namespace="CustomParameters" %>
...
<myNS:UserNameParameter Name="UserName" />
答案 2 :(得分:2)
你真的需要在标签服务器控件中吗?或者你可以只使用服务器控件呈现的span标签吗?
<span runat="server" ID="Label2"><%= User.Identity.Name %></span>
<强>更新强>
好的,新的目标:在不使用代码隐藏的情况下将User.Identity.Name
放入SqlParameter值。
这将是棘手的。基本代码标记蜜蜂(<% %>
等)在您的查询已经执行之后才会在页面生命周期中运行。这意味着您需要自己处理较早的页面生命周期事件,这通常意味着在代码中添加一些内容。如果您真的想摆脱代码隐藏,您当然可以在页面上包含服务器端脚本:
<%@ 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>
但它仍然在编写真正的代码,而不是将所有内容保留在标记中。但我怀疑它是离你最近的地方。
答案 3 :(得分:1)
这会起作用吗?
<asp:LoginName ID="LoginName1" runat="server" />
答案 4 :(得分:1)
我不确定这一点,但我认为
<asp:Label ID="Label1" runat="server" Text='<%# User.Identity.Name %>' />
在属性中定义时,双引号不适用于表达式。 除了使用上面的表达式,您还可以在一些html标签中打印值,只需编写表达式即可。
<span><%# User.Identity.Name %></span>
确保您对用户进行了足够的验证。
答案 5 :(得分:1)
请勿使用自动关闭标签。
<asp:Label ID="UserNameLabel" runat="server" ><%= My.User.Name %></asp:Label>
答案 6 :(得分:0)
<asp:Label ID="Label1" runat="server" Text='<%= User.Identity.Name %>' />
问题是双引号。您经常需要使用单引号
答案 7 :(得分:0)
一种方法是在后面的代码中设置它:
Label1.Text = User.Identity.Name.ToString();
另一种方法是使用表达式构建器(如Ricardo Peres的CodeExpressionBuilder)来绑定aspx标记的控件属性:
<asp:Label runat="server" Text="<%$ Code: User.Identity.Name.ToString() %> />
答案 8 :(得分:0)
我怀疑您忘记在表单上调用DataBind()。 Label3或Label5应该可以正常工作。
在Page_Load()中添加对form1.Databind()的调用,并且应该修复它。
答案 9 :(得分:0)
您可以改用Profile参数:
<asp:ProfileParameter Name="SomeParameter" PropertyName="UserName" Type="String" />
答案 10 :(得分:0)
上面的一些可能的答案,但另一种可能的选择是在Web服务器上使用Windows身份验证并禁用匿名访问。如果将网站设置为本地Intranet,则会自动对Windows用户进行身份验证。
在SQL连接中,使用:Integrated Security = True
重要的是将以下内容添加到web.config:
###
# Compass
###
# Susy grids in Compass
# First: gem install compass-susy-plugin
# require 'susy'
# Change Compass configuration
# require 'compass'
require 'premailer'
compass_config do |config|
config.output_style = :nested
config.line_comments = false
end
set :haml, :attr_wrapper => "\""
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
# page "/path/to/file.html", :layout => false
#
# With alternative layout
# page "/path/to/file.html", :layout => :otherlayout
#
# A path which all have the same layout
# with_layout :admin do
# page "/admin/*"
# end
# Make sure that all partials are rendered without a layout
page "/partials/*", :layout => false
page "/orders.html", :layout => "layout-dealer"
page "/dashboard.html", :layout => "layout-dealer"
# Proxy (fake) files
# page "/this-page-has-no-template.html", :proxy => "/template-file.html" do
# @which_fake_page = "Rendering a fake page with a variable"
# end
###
# Helpers
###
# set :attr_wrapper, "\""
# Refresh pages automatically
activate :livereload
# Automatic image dimensions on image_tag helper
activate :automatic_image_sizes
# Methods defined in the helpers block are available in templates
helpers do
# Print an array of string values as a text list
def text_list(listtext,sep1=", ", sep2=" or ")
n = listtext.size
if n > 1
if n == 2
(listtext.first(n-1)).join(sep1) + sep2 + listtext.last
else
(listtext.first(n-1)).join(sep1) + sep1 + sep2 + listtext.last
end
else
listtext.first
end
end
end
set :css_dir, 'assets/css'
set :js_dir, 'assets/js'
set :images_dir, 'assets/images'
module PreMailer
class << self
def registered(app)
require "premailer"
app.after_build do |builder|
prefix = build_dir + File::SEPARATOR
Dir.chdir(build_dir) do
Dir.glob('email-template.html') do |file|
premailer = Premailer.new(file, :warn_level => Premailer::Warnings::SAFE, :adapter => :nokogiri, :preserve_styles => true, :remove_comments => true, :remove_ids => true)
fileout = File.open(file, "w")
fileout.puts premailer.to_inline_css
fileout.close
# FileUtils.cp file, prefix+'email-build/'
premailer.warnings.each do |w|
builder.say_status :premailer, "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}"
end
builder.say_status :premailer, prefix+file
end
end
end
end
alias :included :registered
end
end
::Middleman::Extensions.register(:inline_premailer, PreMailer)
# activate :inline_premailer
# Build-specific configuration
set :build_dir, "public"
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript, :ignore => /locale/
# Enable cache buster
# activate :cache_buster
# Use relative URLs
activate :relative_assets
# Compress PNGs after build
# First: gem install middleman-smusher
# require "middleman-smusher"
# activate :smusher
# Or use a different image path
# set :http_path, "/Content/images/"
# ignore the build directory for emails
ignore 'email-build/*'
ignore 'node_modules/*'
ignore 'server.coffee'
ignore 'web.config'
end
如果没有web.config中的模拟,您的数据库连接将通过应用程序池中定义的用户进行身份验证:IIS APPPOOL \ ASP.NET v4
然后在SQL内部,你可以使用CURRENT_USER函数来读取用户,因此不需要通过存储过程传递当前用户。
汤姆