简体   繁体   English

如何在C#中连接两个不同的Web表单?

[英]How can i connecting two different web forms in C#?

I'm working on my school project in which I need to build up one original web application. 我正在开展我的学校项目,我需要在其中构建一个原始的Web应用程序。 But I got an error when I tried to connect two web forms in my project. 但是当我尝试在项目中连接两个Web表单时出现错误。 I have no idea what's wrong about it, and I have looked at many video instruction sources showing how to do it, and still I have the same error. 我不知道它有什么问题,我已经看过很多视频指令来源,显示如何做,但我仍然有同样的错误。 Here is what I have done. 这就是我所做的。

First, I created a web from and named it MainForm and also I made another one and named TestForm1 . 首先,我创建了一个Web并将其命名为MainForm ,我还创建了另一个并命名为TestForm1 Then, I put a button on TestForm1 aspx file, double-clicked the button to call the cs file, and there I coded 然后,我在TestForm1 aspx文件上放了一个按钮,双击按钮调用cs文件,然后我编码了

protected void Button1_Click(object sender, EventArgs e)
{
    MainForm newWindow = new MainForm();
    newWindow.Show();
}

Here, an error comes up and I see a red line under "Show" The error says 在这里,出现错误,我在“显示”下看到一条红线错误说明

MainFor1 does not contain a definition for "Show," and no extension method accepting a first argument of type "MainForm" could be found. MainFor1不包含“Show”的定义,并且没有可以找到接受“MainForm”类型的第一个参数的扩展方法。

What is wrong about my code? 我的代码有什么问题? I just simply made two forms and am trying to connect them. 我只是简单地制作了两种形式,并试图将它们连接起来。 Please tell me how I can handle this problem. 请告诉我如何处理这个问题。

Sorry for my bad English, since I'm not a native speaker. 抱歉我的英语不好,因为我不是母语人士。 And Thank you in advance. 并提前谢谢你。

根据我的Understating你想导航到另一个页面,你可以添加一个链接按钮,带你到另一个页面,如下所示:

 <asp:Button ID="Button3" runat="server" Text="Button" PostBackUrl="anotherpage.aspx" />

To show a new Page (not a Form!) means that you want the Web Browser to open a new URL. 显示新页面(不是表单!)意味着您希望Web浏览器打开新URL。 This can be done in many ways, and the way closest to your orignal code is this: 这可以通过多种方式完成,最接近您的orignal代码的方式是:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("MainForm.aspx");
}

A Redirect literally tells the browser: please go to another URL. Redirect字面上告诉浏览器:请转到另一个URL。 The browser then makes a new http request with the new URL, and the server then shows that page. 然后,浏览器使用新URL发出新的http请求,然后服务器显示该页面。

You need to use Server.Transfer() , method Show() can't be used in WebForms. 您需要使用Server.Transfer() ,方法Show()不能在WebForms中使用。 Probably You are wrong WinFroms with WebForms. 可能你错误​​的WinFroms与WebForms。

  Server.Transfer("TestForm1.aspx", true);

Here you find introduction to ASP.NET and Web Forms 在这里,您可以找到ASP.NET和Web窗体的介绍

Summary: This article explains how Web Forms are fundamental to Microsoft ASP.NET, shows how to build a Web form, and discusses the controls essential for building a Web Form. 简介:本文解释了Web窗体如何成为Microsoft ASP.NET的基础,展示了如何构建Web窗体,并讨论了构建Web窗体所必需的控件。 (16 printed pages) (16页打印)

Objectives 目标

  • Learn about Web Forms 了解Web表单
  • Learn the Web controls that are built into Web Forms 了解Web窗体中内置的Web控件
  • Build a Web Form 构建Web表单

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

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