简体   繁体   English

将Aspx页面从VB转换为CS

[英]Converting Aspx page from VB to CS

I have an asp.net page WineCompDefault.aspx which was initially implemented with VB code behind. 我有一个asp.net页面WineCompDefault.aspx,该页面最初是通过VB代码实现的。 I am now trying to change the code behind to CS. 我现在正在尝试将背后的代码更改为CS。 I modified the page directive in the aspx page as follows: 我修改了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" %>

The WineCompDefault.aspx.vb file contained the following: 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

And the code converter from VB to C# gave the following (except I changed the class name to WineCompDefaultCS which I also changed in the page directive, so I could have both code behind files in the project) 从VB到C#的代码转换器提供了以下内容(除了我将类名更改为WineCompDefaultCS,而且我还在page指令中进行了更改,因此我可以将两个代码都放在项目中的文件后面)

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;
    }
}

The problem is it says "The type or namespace Public could not be found". 问题是它说“找不到公共的类型或名称空间”。 I do not know what is wrong or missing. 我不知道什么是错的或缺失的。 All help greatly appreciated. 所有帮助,不胜感激。

(Also, note, I will remove the VB file once I am sure the CS is working correctly. That is why I would like to keep both in the project at this time) (此外,请注意,一旦我确定CS可以正常工作,我将删除VB文件。这就是为什么我现在希望将两者都保留在项目中的原因)

Your constructor is incorrect. 您的构造函数不正确。 The constructor must always match the class name ( WineCompDefaultCS ). 构造函数必须始终与类名( WineCompDefaultCS )匹配。 In addition, c# is case sensitive and access modifiers such as public should be lower case. 另外,c#区分大小写,访问修饰符(例如public应小写。

Change this: 更改此:

Public WineCompDefault()
{
    Load += Page_Load;
}

To this: 对此:

public WineCompDefaultCS()
{
    Load += Page_Load;
}

You renamed the class but not the constructor. 您重命名了该类,但未重命名该构造函数。 In C#, the constructor must match the class name. 在C#中,构造函数必须与类名匹配。

WineCompDefault should be WineCompDefaultCS WineCompDefault应该是WineCompDefaultCS

An old question - but this is what I could find till now - 一个古老的问题-但这是我到目前为止所能找到的-

You need to delete the file and add it back using Add->New File (this time select the c#) and then paste back the aspx (with lang="C#" and CodeFile="WineCompDefault.aspx.cs") 您需要删除文件,然后使用“添加”->“新文件”(这次选择c#)将其添加回去,然后粘贴aspx(使用lang =“ C#”和CodeFile =“ WineCompDefault.aspx.cs”)

I don't know why or how Visual Studio knows about the way the page was initially converted - I couldn't find out where VS stores this information. 我不知道Visual Studio为什么或如何知道页面最初转换的方式-我无法确定VS在何处存储此信息。

I hope this helps! 我希望这有帮助!

您缺少退货声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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