开发者

Getting a UserId into a SQLDataSource

I am still new to asp.net and I'm having a problem that I just can't figure out. I'm using vb and the .net membership api.

My question is, how do开发者_JAVA技巧 I get the current user's userid into a DetailsView INSERT?

<InsertParameters>
<asp:Parameter Name="UserID"/>
</InsertParameters>


In the OnInserting event of your SqlDataSource you can retrieve the UserID from the Membership table by using the ProviderUserKey property of the MembershipUser class. You can then programmatically assign this value to the UserID insert parameter.

protected void SqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e) 
{      
    MembershipUser currentUser = Membership.GetUser();      
    Guid id = (Guid)currentUser.ProviderUserKey;
    e.Command.Parameters["@UserID"].Value = id; 
}


User.Identity.Name will get you the unique user name. You can find the id in the aspnet_Users table.


Just do it like this in one line:

protected void Sql_OnInserting(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters["@UserId"].Value = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜