locked
Insert SystemUsername into formview text box. RRS feed

  • Question

  • User-1511902651 posted

    Hello,

    I am using formview to pull the data from DB for user to view and edit.  I would like to be able to insert system date and username into the DB if user edits the row.  I have placed an insert statement into "Update" section of the datasource so that all fields are pre generated when user clicks edit and when submitted it creates a new row in the DB so we can record change history.    so I would like to generate the username and updated date fields with system date and system username.  any help is appreciated.  thanks

    Friday, December 28, 2012 1:08 PM

All replies

  • User-1716253493 posted
    try : User.Identity.Name and DateTime.Now.ToShortDateString() or Now.Date()
    Friday, December 28, 2012 1:37 PM
  • User-1511902651 posted

    How do I bind User.Identit.Name to a text box in the formview?  I can do it if the textbox is outside og the formview. 

    Friday, December 28, 2012 1:42 PM
  • User-1716253493 posted
    Textbox tb = (Textbox)Formview1.findcontrol("tb1") ... tb.Text=...
    Friday, December 28, 2012 1:52 PM
  • User-1511902651 posted

    didnt work.  

    This is what I have so far.

    Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
    Select Case FormView1.CurrentMode
    Case FormViewMode.Edit

    Textbox textbox2 = (Textbox)Formview1.findcontrol("textbox2") textbox2.Text= User.Identity.Name

    End Select
    End Sub

    Friday, December 28, 2012 2:14 PM
  • User-1511902651 posted

    I also tried below and text box is still blank.  

    <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# User.Identity.Name %>' />

    Friday, December 28, 2012 2:29 PM
  • User-1716253493 posted
    <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />

    In modechanged event

    Protected Sub FormView1_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
                                                            Handles FormView1.ModeChanged
            If FormView1.DefaultMode = FormViewMode.Insert Then
                Dim tb As TextBox = CType(FormView1.FindControl("UsernameTextBox"), TextBox)
                tb.Text = User.Identity.Name
            End If
    End Sub



    Friday, December 28, 2012 10:22 PM
  • User-1511902651 posted

    I cant get it to work.   not sure why.  here is my code.  It just wont generate the username in the text box so when I enter update it wont write it to SQL

    Protected Sub FormView1_DataBound(sender As Object, e As EventArgs)
            If FormView1.CurrentMode = FormViewMode.Edit Then
                Dim tb As TextBox = DirectCast(FormView1.FindControl
    ("UsernameTextBox"), TextBox)
                tb.Text = User.Identity.Name
            End If
        End Sub
    
    
    
    
    <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
                    <br />




    Wednesday, January 2, 2013 10:26 AM