将Aspx页面从VB转换为CS

本文关键字:转换 CS VB Aspx | 更新日期: 2023-09-27 18:14:08

我有一个asp.net页面WineCompDefault。它最初是用VB代码实现的。我现在正试图改变背后的代码CS。我修改了aspx页面中的page指令如下:

From -

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="WineCompDefault.aspx.vb" Inherits="WineCompDefault" %>

To -

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="WineCompDefault.aspx.cs" Inherits="WineCompDefaultCS" %>

WineCompDefault.aspx.vb文件包含以下内容:

Imports Microsoft.VisualBasic
Imports System.Web.UI
Imports MarymonteDAL
Imports System.Data
Imports System.Data.OleDb

Partial Class WineCompDefault
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Protected Sub btnLogOn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogOn.Click
        Dim myLabel As Label
        myLabel = Page.FindControl("lblTitle")
        If Not myLabel Is Nothing Then
            lblResults.Text = myLabel.Text
        Else
            lblResults.Text = "Could not find the label control."
        End If
        btnLogOn.Visible = False
    End Sub
End Class

从VB到c#的代码转换器给出了以下内容(除了我将类名称更改为WineCompDefaultCS,我也在页面指令中更改了它,所以我可以在项目中拥有两个文件后面的代码)

using Microsoft.VisualBasic;
using System.Web.UI;
using System.Data;
using System.Data.OleDb;

partial class WineCompDefaultCS : System.Web.UI.Page
{

    protected void Page_Load(object sender, System.EventArgs e)
    {
    }
    protected void btnLogOn_Click(object sender, System.EventArgs e)
    {
        Label myLabel = default(Label);
        myLabel = Page.FindControl("lblTitle");
        if ((myLabel != null)) {
            lblResults.Text = myLabel.Text;
        } else {
            lblResults.Text = "Could not find the label control.";
        }
        btnLogOn.Visible = false;
    }
    Public WineCompDefault()
    {
        Load += Page_Load;
    }
}

问题是它说"类型或命名空间Public找不到"。我不知道什么是错的或错过了。谢谢大家的帮助。

(另外,请注意,一旦我确定CS工作正常,我将删除VB文件。这就是为什么我想让这两个项目在这个时候)

将Aspx页面从VB转换为CS

您重命名了类,但没有重命名构造函数。在c#中,构造函数必须匹配类名。

WineCompDefault应该是WineCompDefaultCS

构造函数不正确。构造函数必须始终匹配类名(WineCompDefaultCS)。另外,c#是区分大小写的,像public这样的访问修饰符应该是小写的。

改变:

Public WineCompDefault()
{
    Load += Page_Load;
}

:

public WineCompDefaultCS()
{
    Load += Page_Load;
}

一个老问题-但这是我现在能找到的-

你需要删除文件并使用add ->New file(这次选择c#)将其添加回来,然后粘贴回aspx (lang=" c# " and CodeFile="WineCompDefault.aspx.cs")

我不知道为什么或如何Visual Studio知道页面最初转换的方式-我找不到VS存储这些信息的地方。

我希望这对你有帮助!

您缺少一个返回语句