I am having difficulty automatically adding Windows username into a Listview in Insert Mode.
I have a listview that allows one to insert, edit and delete data from a SQL Server 2008 database. While inserting, the user adds info in the columns, which is then added into a database. One of the fields is CreatedBy - right now, the user adds is or her
name there. I would like the Windows username (the one user employs to log in to their PC) to be automatically added to the Created By field.
We have Windows authentication enabled, and the user logs in using their Active Directory username and password. I just can't seem to capture the logged in username and have it show up in the "Created By" field.
Here is the code that runs when the Listview is loaded:
Private Property CurrentMode As FormViewMode
Protected Sub ListView1_Load(sender As Object, e As EventArgs) Handles Me.Load
If ListView1.ViewStateMode = FormViewMode.Insert Then
Dim CurrentUser As TextBox = CType(ListView1.FindControl("CreatedByTextBox"), TextBox)
CurrentUser.Text = Right(User.Identity.Name.ToString, Len(User.Identity.Name.ToString) - 11)
CurrentUser.Enabled = False
End If
End Sub
The domain name is 10 characters long - since I only want the username, I have subtracted 11 from the full length of the username above ("DOMAIN\User" - I only want User in the Created By field, not "DOMAIN\", which is 11 characters long).
Part of ASPX code for InsertItemTemplate for this listview, pertaining to CreatedBy field, is as follows:
Created By:
<asp:TextBox ID="CreatedByTextBox" runat="server" Width="120px"
Text='<%# Bind("CreatedBy") %>' />
I suspect the problem is in VB code above - but I don't know. Any help will be much appreciated -
j-d
Member
13 Points
5 Posts
Listview Insert - How to add Windows username in textbox
May 29, 2012 03:55 PM|LINK
I am having difficulty automatically adding Windows username into a Listview in Insert Mode.
I have a listview that allows one to insert, edit and delete data from a SQL Server 2008 database. While inserting, the user adds info in the columns, which is then added into a database. One of the fields is CreatedBy - right now, the user adds is or her name there. I would like the Windows username (the one user employs to log in to their PC) to be automatically added to the Created By field.
We have Windows authentication enabled, and the user logs in using their Active Directory username and password. I just can't seem to capture the logged in username and have it show up in the "Created By" field.
Here is the code that runs when the Listview is loaded:
Private Property CurrentMode As FormViewMode Protected Sub ListView1_Load(sender As Object, e As EventArgs) Handles Me.Load If ListView1.ViewStateMode = FormViewMode.Insert Then Dim CurrentUser As TextBox = CType(ListView1.FindControl("CreatedByTextBox"), TextBox) CurrentUser.Text = Right(User.Identity.Name.ToString, Len(User.Identity.Name.ToString) - 11) CurrentUser.Enabled = False End If End SubThe domain name is 10 characters long - since I only want the username, I have subtracted 11 from the full length of the username above ("DOMAIN\User" - I only want User in the Created By field, not "DOMAIN\", which is 11 characters long).
Part of ASPX code for InsertItemTemplate for this listview, pertaining to CreatedBy field, is as follows:
Created By: <asp:TextBox ID="CreatedByTextBox" runat="server" Width="120px" Text='<%# Bind("CreatedBy") %>' />I suspect the problem is in VB code above - but I don't know. Any help will be much appreciated -
Much thanks
JayDee
jm
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Listview Insert - How to add Windows username in textbox
May 31, 2012 08:40 AM|LINK
Hi,
You can handle the DataBound event of ListView to set the name to the TextBox. Something like this:
Dim tb As TextBox = DirectCast(ListView1.InsertItem.FindCotnrol("CreatedByTextBox"), TextBox)
'set the name to this txt
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework