从Login Control获取UserName值

时间:2015-09-13 13:36:31

标签: asp.net vb.net

尝试获取ASP.net登录控件中UserName字段的值。这是我的.net:

<asp:Login ID="Login1" runat="server" BackColor="#FFFBD6" BorderColor="#FFDFAD" BorderPadding="4"
                BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
                ForeColor="#333333" TextLayout="TextOnTop" DestinationPageUrl="~/Receipt.aspx" 
                UserNameLabelText="User Name (From: xxxxx.com):" Height="105px" PasswordRecoveryText="Forgot User Name or Password?" PasswordRecoveryUrl="~/GetPassword.aspx">
            <LoginButtonStyle BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px"
                Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" />
            <TextBoxStyle Font-Size="0.8em" />
            <TitleTextStyle BackColor="#990000" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            </asp:Login>

这是我的vb,我将值存储在txt文件中 - 运行时没有错误,但也没有输入任何值。

Imports System.IO
Imports System.Data

Partial Class Login
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim xUserName As String = User.Identity.Name()
    'UpdateLastLoginDate(xUserName)
End Sub

Protected Sub LoginButton_Click(sender As Object, e As EventArgs)
    Dim username As String = Request.Form("UserName")
    Dim filepath As String = Server.MapPath("~") + "debug.txt"

    Using writer As StreamWriter = File.CreateText(filepath)
        writer.WriteLine(username)
    End Using
End Sub

End Class

2 个答案:

答案 0 :(得分:0)

您需要使用ASP.NET Dim username As String = Login1.UserName; 登录控件,如

man qemu

答案 1 :(得分:0)

以更精确的方式转移到新问题: Call a function from the Login Control in vb.net

工作结果是:

Protected Sub Page_Load(sender As Object, e As EventArgs)
    WireLoginControlButtonClickEvent(Login1)
End Sub

Private Sub WireLoginControlButtonClickEvent(parent As Control)
    For Each ctrl As Control In parent.Controls
        If TypeOf ctrl Is Button Then
            AddHandler DirectCast(ctrl, Button).Click, AddressOf MyFunction
        ElseIf ctrl.HasControls() Then
            WireLoginControlButtonClickEvent(ctrl)
        End If
    Next
End Sub

Private Sub MyFunction(sender As Object, e As EventArgs)
    Dim username As String = Login1.UserName
    Dim currentDateTime As DateTime = DateTime.Now.AddHours(3)
    Dim filepath As String = Server.MapPath("~") + "\debug.txt"

    Using writer As StreamWriter = File.AppendText(filepath)
        writer.WriteLine(username)
        writer.WriteLine(currentDateTime)
        writer.WriteLine("")
    End Using
End Sub