Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 31, 2012 08:40 AM by Qin Dian Tang - MSFT
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
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:
<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 -
May 31, 2012 08:40 AM|LINK
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