如何使用User.Identity.Name在ASP.NET SqlDataSource的为一个参数? [英] How to use User.Identity.Name as a parameter for SqlDataSource in ASP.NET?

查看:206
本文介绍了如何使用User.Identity.Name在ASP.NET SqlDataSource的为一个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的SqlDataSource我可以配置传入paramater外部源。
例如,它可能是一个查询字符串,会议,个人资料等。
但是我没有使用用户作为源的选项。

我知道我可以在插入的参数提供价值,选择,更新,删除事件。但我不认为这是一个ellegant解决方案,因为我有一些aspx文件已经definied parameteres。我不希望在两个不同的地方定义的参数。它使一团糟。

所以我可以采用某种定义.aspx文件中这个参数?

 < SelectParameters>
        < ASP:QueryStringParameter默认值= - 1NAME =ID
            QueryStringField =ID/>
        //User.Identity.Name放在这里作为另一个参数的值
    < / SelectParameters>


解决方案

声明它在你的的.aspx 并在codebehind填充:

ASPX

 < ASP:参数名称=用户名类型=字符串默认值=无名氏/>

codebehind

 保护无效Page_Init(对象发件人,EventArgs的发送){
    DataSource.SelectParameters [用户名] =默认值User.Identity.Name。
}

For SqlDataSource I can configure the external source for the incoming paramater. For example it might be a QueryString, Session, Profile and so on. However I do not have an option to use User as a source.

I know that I could provide value for the parameter in Inserting,Selecting,Updating,Deleting events. But I do not think that this is an ellegant solution because I have some parameteres already definied in aspx file. I do not want to have parameters defined in two separate places. It makes mess.

So can I somehow define this parameter in .aspx file?

    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="-1" Name="ID" 
            QueryStringField="ID" />
        //User.Identity.Name goes here as a value for another parameter  
    </SelectParameters> 

解决方案

Declare it in your .aspx and fill it in your codebehind:

aspx

<asp:Parameter Name="username" Type="String" DefaultValue="Anonymous" />

codebehind

protected void Page_Init(object sender, EventArgs e) {
    DataSource.SelectParameters["username"].DefaultValue = User.Identity.Name;
}

这篇关于如何使用User.Identity.Name在ASP.NET SqlDataSource的为一个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆