简体   繁体   中英

Trying to convert from C# to VB.net

I am trying to change this C# code to VB.net and am running into an issue with the username select box not populating with the list of membership users. The C# code properly displays the users list.

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_change_change : System.Web.UI.Page
{

MembershipUserCollection users;

protected void Page_LoadComplete(object sender, EventArgs e)
{
    users = Membership.GetAllUsers();

    if (!IsPostBack)
    {
        UserName.DataSource = users;
        UserName.DataBind();
    }
}

protected void btnChange_Click(object sender, EventArgs e)
{
    MembershipUser userToChange = Membership.GetUser(UserName.SelectedItem.Value);

    if (userToChange != null)
    {
        userToChange.ChangePassword(userToChange.ResetPassword(), NewPassword.Text);
        Response.Redirect("/admin/");
    }
    else
    {
        message.Text = "Invalid Username or Password";
    }
}

protected void btnGoBack_Click(object sender, EventArgs e)
{
    Response.Redirect("/admin/");
}

}

VB.NET

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Partial Class admin_change_change
Inherits System.Web.UI.Page

Private users As MembershipUserCollection

Protected Sub Page_LoadComplete(sender As Object, e As EventArgs)
    users = Membership.GetAllUsers()

    If Not IsPostBack Then
        UserName.DataSource = users
        UserName.DataBind()
    End If
End Sub

Protected Sub btnChange_Click(sender As Object, e As EventArgs)
    Dim userToChange As MembershipUser = Membership.GetUser(UserName.SelectedItem.Value)

    If userToChange IsNot Nothing Then
        userToChange.ChangePassword(userToChange.ResetPassword(), NewPassword.Text)
        Response.Redirect("/admin/")
    Else
        message.Text = "Invalid Username or Password"
    End If
End Sub

Protected Sub btnGoBack_Click(sender As Object, e As EventArgs)
    Response.Redirect("/admin/")
End Sub

End Class

Your event handler is missing the Handles Me.LoadComplete at the end of it:

Protected Sub Page_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete
    users = Membership.GetAllUsers()

    If Not IsPostBack Then
        UserName.DataSource = users
        UserName.DataBind()
    End If
End Sub

By default, AutoEventWireup is false in ASP.Net VB. As the result, you need to explicity attach the event like roryap suggested.

<%@ Page AutoEventWireup="false" %>

However, if you do not want to attach manually, you can make AutoEventWireup="true" which is default in ASP.net C#.

Make sure Page_LoadComplete should be renamed to Page_Load which is a stand event name.

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Dim users = Membership.GetAllUsers()
        UserName.DataSource = users
        UserName.DataBind()
    End If
End Sub

In addition, users should be inside if statement if its value is not used in the rest of the page to avoid unnecessary data retrieving from DataBase.

Here is the free VB to C# code converter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM