调用从登录控制函数在vb.net [英] Call a function from the Login Control in vb.net

查看:95
本文介绍了调用从登录控制函数在vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图调用后面code功能,但不工作 - 一个建议是使用的 OnAuthenticate 不是的OnClick 然而,这需要整个认证过程的改写。

 < ASP:登录ID =Login1=服务器的OnClick =用作MyButtonDestinationPageUrl =〜/ Receipt.aspx
   UserNameLabelText =用户名(来源:mysite.com):
   PasswordRecoveryText =忘记用户名或密码?
   PasswordRecoveryUrl =〜/ GetPassword.aspx>
< / ASP:登录>

VB code:

 保护小组用作MyButton(BYVAL发件人为对象,BYVAL E上System.EventArgs)
    昏暗的用户名作为字符串= Login1.UserName
    昏暗currentDateTime为DATETIME = DateTime.Now.AddHours(3)
    昏暗的文件路径作为字符串=使用Server.Mappath(〜)+\\ DEBUG.TXT    使用作家作为的StreamWriter = File.AppendText(文件路径)
        writer.WriteLine(用户名)
        writer.WriteLine(currentDateTime)
        writer.WriteLine()
    使用完
结束小组

修订code:

 进口System.IO
进口System.Data这部分类登录
继承System.Web.UI.Page保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
    昏暗xUserName作为字符串= User.Identity.Name()
    UpdateLastLoginDate(xUserName)
结束小组保护小组用作MyButton(BYVAL发件人为对象,BYVAL E上System.EventArgs)    昏暗的用户名作为字符串= Login1.UserName
    昏暗PW作为字符串= Login1.Password
    昏暗currentDateTime为DATETIME = DateTime.Now.AddHours(3)
    昏暗的文件路径作为字符串=使用Server.Mappath(〜)+\\ DEBUG.TXT    使用作家作为的StreamWriter = File.AppendText(文件路径)
        writer.WriteLine(用户名)
        writer.WriteLine(PW)
        writer.WriteLine(currentDateTime)
        writer.WriteLine(REMOTE_ADDR:+ Request.Servervariables(REMOTE_ADDR))
        writer.WriteLine(USER_AGENT:+ Request.Servervariables(HTTP_USER_AGENT))
        writer.WriteLine(LOCAL_ADDR:+ Request.Servervariables(LOCAL_ADDR))
        writer.WriteLine(LOGON_USER:+ Request.Servervariables(LOGON_USER))
        'writer.WriteLine(Request.Servervariables(ALL_HTTP))
        writer.WriteLine()
    使用完
结束小组保护小组Page_ preINIT(发送者为对象,E作为EventArgs的)
    WireLoginControlButtonClickEvent(Login1)
结束小组私人小组WireLoginControlButtonClickEvent(父母作为对照)
    对于每Ctrl键控制在parent.Controls
        如果TypeOf运算CTRL为按钮,然后
            的AddHandler DirectCast(Ctrl键,按钮)。单击,()的函数(MyFunction的())
        elseif的ctrl.HasControls()然后
            WireLoginControlButtonClickEvent(CTRL)
        万一
    下一个
结束小组私有函数MyFunction的()作为字符串
    的Response.Write(函数调用)
    什么都不返回
结束功能末级


解决方案

试试这个:

 保护小组Page_ preINIT(发送者为对象,E作为EventArgs的)
    WireLoginControlButtonClickEvent(Login1)
结束小组私人小组WireLoginControlButtonClickEvent(父母作为对照)
    对于每Ctrl键控制在parent.Controls
        如果TypeOf运算CTRL为按钮,然后
            的AddHandler DirectCast(Ctrl键,按钮)。点击,AddressOf的MyFunction
        elseif的ctrl.HasControls()然后
            WireLoginControlButtonClickEvent(CTRL)
        万一
    下一个
结束小组私人小组MyFunction的(发送者为对象,E作为EventArgs的)
    的Response.Write(函数调用)
结束小组

Trying to call a function in code behind but not working - one suggestion was to use the OnAuthenticate instead of the OnClick however this requires a rewrite of the entire authentication process.

<asp:Login ID="Login1" runat="server" OnClick="MyButton"  DestinationPageUrl="~/Receipt.aspx" 
   UserNameLabelText="User Name (From: mysite.com):" 
   PasswordRecoveryText="Forgot User Name or Password?" 
   PasswordRecoveryUrl="~/GetPassword.aspx">
</asp:Login>

vb code:

Protected Sub MyButton(ByVal sender As Object, ByVal e As System.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 

Revised Code:

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 MyButton(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim username As String = Login1.UserName
    Dim pw As String = Login1.Password
    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(pw)
        writer.WriteLine(currentDateTime)
        writer.WriteLine("REMOTE_ADDR: " + Request.Servervariables("REMOTE_ADDR"))
        writer.WriteLine("USER_AGENT: " + Request.Servervariables("HTTP_USER_AGENT"))
        writer.WriteLine("LOCAL_ADDR: " + Request.Servervariables("LOCAL_ADDR"))
        writer.WriteLine("LOGON_USER: " + Request.Servervariables("LOGON_USER"))
        'writer.WriteLine(Request.Servervariables("ALL_HTTP"))
        writer.WriteLine("")
    End Using
End Sub 

Protected Sub Page_PreInit(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, Function() (MyFunction())
        ElseIf ctrl.HasControls() Then
            WireLoginControlButtonClickEvent(ctrl)
        End If
    Next
End Sub

Private Function MyFunction() As String
    Response.Write("Function Called")
    Return Nothing
End Function

End Class

解决方案

Give this a try:

Protected Sub Page_PreInit(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)
    Response.Write("Function Called")
End Sub

这篇关于调用从登录控制函数在vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆