aspx 页面无法与 aspx.cs 文件连接

Posted

技术标签:

【中文标题】aspx 页面无法与 aspx.cs 文件连接【英文标题】:aspx page could not connected with aspx.cs file 【发布时间】:2017-08-31 04:49:58 【问题描述】:

所以基本上我有一个名为 Lab4.aspx 的网页表单,但每次我按下按钮时,它总是会指向脚本 runat="server" 而不是 aspx.cs,当我更改按钮时,按钮也会更改回 Button_1到 Btn 提交

<script runat="server">

    Protected Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub
</script>

对于 aspx.cs,我已经在 aspx.cs 中将公共部分类更改为公共部分类 Lab4: System.Web.UI.Page 并将文件命名为 Lab4.aspx.cs

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

public partial class Lab4 : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e)
    

    
    protected void BtnSubmit_Click(object sender, EventArgs e)

如果能解开这个谜,那就太好了,因为我已经尝试过到处寻找这个解决方案

这是aspx页面上按钮控件的代码

<asp:Button ID="BtnSubmit" runat="server" onclick="Button1_Click" Text="Submit" 
                        Width="85px" />

这是aspx页面的完整代码

<script runat="server">

    Protected Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        
            font-size: xx-large;
            font-family: "Tw Cen MT";
            height: 1161px;
        
        .style2
        
            text-decoration: underline;
        
        .style3
        
            font-size: x-large;
            background-color: #9933FF;
        
        .style4
        
            width: 100%;
        
        .style5
        
            width: 243px;
        
        .style6
        
            width: 609px;
        
        .style7
        
            width: 243px;
            height: 32px;
        
        .style8
        
            width: 609px;
            height: 32px;
            background-color: #FFFFFF;
        
        .style9
        
            height: 32px;
        
        .style10
        
            background-color: #9966FF;
        
        .style11
        
            width: 243px;
            height: 31px;
        
        .style12
        
            width: 609px;
            height: 31px;
        
        .style13
        
            height: 31px;
        
        .style14
        
            width: 303px;
            font-size: x-large;
        
        .style15
        
            width: 303px;
            height: 41px;
        
        .style16
        
            height: 41px;
        
        .style17
        
            font-size: x-large;
        
        .style20
        
            width: 254px;
        
        .style21
        
            width: 254px;
            height: 41px;
        
        .style22
        
            width: 243px;
            height: 45px;
        
        .style23
        
            width: 609px;
            height: 45px;
        
        .style24
        
            height: 45px;
        
        .style25
        
            width: 303px;
            font-size: x-large;
            height: 32px;
        
        .style26
        
            width: 254px;
            height: 32px;
        
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="style1">

        Nasi Tapau O<span class="style2">nline Order&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </span>
        <br />
        <br />
        <span class="style3"><span class="style10">Customer&#39;s Details&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </span>
        <br />
        <table class="style4">
            <tr>
                <td class="style5">
                    Name : 
                </td>
                <td class="style6">
                    <span class="style3">
                    <asp:TextBox ID="TxtName" runat="server" Width="545px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                </td>
                <td class="style8">
                </td>
                <td class="style9">
                </td>
                <td class="style9">
                </td>
            </tr>
            <tr>
                <td class="style5">
                    Contact Number :</td>
                <td class="style6">
                    <span class="style3">
                    <asp:TextBox ID="TxtContactNo" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    Email :</td>
                <td class="style6">
                    <span class="style3">
                    <asp:TextBox ID="TxtEmail" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                </td>
                <td class="style8">
                </td>
                <td class="style9">
                </td>
                <td class="style9">
                </td>
            </tr>
            <tr>
                <td class="style5">
                    Order Details :</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    Quantity :
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    Rice :</td>
                <td class="style6">
                    <asp:RadioButton ID="RBYes" runat="server" Text="Yes" />
&nbsp;<span class="style3"><asp:RadioButton ID="RBNo" runat="server" 
                        style="background-color: #FFFFFF" Text="No" />
                    </span>
                </td>
                <td>
                    <span class="style3">
                    <asp:TextBox ID="TxtQRice" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style22">
                </td>
                <td class="style23">
                </td>
                <td class="style24">
                </td>
                <td class="style24">
                </td>
            </tr>
            <tr>
                <td class="style5">
                    Side Dishes :</td>
                <td class="style6">
                    <asp:CheckBox ID="CBChicken" runat="server" style="background-color: #FFFFFF" Text="Chicken Curry - RM4.50" />
                </td>
                <td>
                    <span class="style3">
                    <asp:TextBox ID="TxtQChicken" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                </td>
                <td class="style8">
        <span class="style3">
                    <asp:CheckBox ID="CBMixed" runat="server" OnCheckedChanged="CBMixed_CheckedChanged" style="background-color: #FFFFFF" Text="Mixed Vegetables - RM2.50" />
        </span>
                </td>
                <td class="style9">
                    <span class="style3">
                    <asp:TextBox ID="TxtQMixed" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td class="style9">
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
        <span class="style3">
                    <asp:CheckBox ID="CBGrilled" runat="server" style="background-color: #FFFFFF" Text="Grilled Fish - RM4.00" />
        </span>
                </td>
                <td>
                    <span class="style3">
                    <asp:TextBox ID="TxtQGrilled" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                </td>
                <td class="style8">
        <span class="style3">
                    <asp:CheckBox ID="CBCondiment" runat="server" style="background-color: #FFFFFF" Text="Condiment - RM1.50" />
        </span>
                </td>
                <td class="style9">
                    <span class="style3">
                    <asp:TextBox ID="TxtQCondiment" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td class="style9">
                </td>
            </tr>
            <tr>
                <td class="style5">
                    Beverages :</td>
                <td class="style6">
        <span class="style3">
                    <asp:CheckBox ID="CBMangoJ" runat="server" style="background-color: #FFFFFF" Text="Mango Juice - RM3.00" />
        </span>
                </td>
                <td>
                    <span class="style3">
                    <asp:TextBox ID="TxtMangoJuice" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
        <span class="style3">
                    <asp:CheckBox ID="CBCendol" runat="server" style="background-color: #FFFFFF" Text="Cendol - RM3.00" />
        </span>
                </td>
                <td>
                    <span class="style3">
                    <asp:TextBox ID="TxtCendol" runat="server" Width="165px"></asp:TextBox>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style11">
                    Delivery:</td>
                <td class="style12">
                    <asp:DropDownList ID="DdlDelivery" runat="server">
                        <asp:ListItem Value="0">Self-pickup</asp:ListItem>
                        <asp:ListItem Value="1.50">Request For Delivery</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="style13">
                </td>
                <td class="style13">
                </td>
            </tr>
            <tr>
                <td class="style11">
                    &nbsp;</td>
                <td class="style12">
                    &nbsp;</td>
                <td class="style13">
                    <asp:Button ID="BtnSubmit" runat="server" onclick="Button1_Click" Text="Submit" 
                        Width="85px" />
                </td>
                <td class="style13">
                    &nbsp;</td>
            </tr>
        </table>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" style="background-color: #9966FF" 
            Text="Thank you for choosing us! Here are you order details :"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
        <br />
        </span>
        <table class="style4">
            <tr>
                <td class="style14">
                    <span class="style17">Name :
                </td>
                <td class="style20">
                    <asp:Label ID="lblName" runat="server" Font-Size="X-Large"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Telephone :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="LblTel" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Email :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblEmail" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td style="font-size: x-large">
                    Quantity :</td>
            </tr>
            <tr>
                <td class="style25">
                    Rice :</td>
                <td class="style26">
                    <span class="style17">
                    <asp:Label ID="lblRice" runat="server"></asp:Label>
                    </span>
                </td>
                <td class="style9">
                    <span class="style17">
                    <asp:Label ID="lblQRice" runat="server"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Side Dishes :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="Label6" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
                <td>
                    <span class="style17">
                    <asp:Label ID="Label16" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="Label7" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
                <td>
                    <span class="style17">
                    <asp:Label ID="Label17" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style15">
                </td>
                <td class="style21">
                    <span class="style17">
                    <asp:Label ID="Label8" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
                <td class="style16">
                    <span class="style17">
                    <asp:Label ID="Label18" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="Label9" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
                <td>
                    <span class="style17">
                    <asp:Label ID="Label19" runat="server" Visible="False"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    Beverages :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblMangoJ" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    <span class="style17">
                    <asp:Label ID="lblQMangoJ" runat="server"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblCendol" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    <span class="style17">
                    <asp:Label ID="lblQCendol" runat="server"></asp:Label>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Delivery Method :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblDelivery" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Total :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblTotal" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    Grand Total(Inclusive GST) :</td>
                <td class="style20">
                    <span class="style17">
                    <asp:Label ID="lblTotalWGST" runat="server"></asp:Label>
                    </span>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style14">
                    &nbsp;</td>
                <td class="style20">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        <br />
        <br />

    </div>
    </form>
</body>
</html>

【问题讨论】:

使用代码,而不是图像 你能给你看一下 aspx 页面吗,我确定你在页面指令中遗漏了一些东西 请发布aspx页面完整代码 可能是页面标签有问题 你能把你的aspx页面的第一行贴出来吗?不是 aspx.cs 【参考方案1】:

您的 aspx 页面实际上应该以这样的方式开始。

<%@ Page Title="" Language="C#" MasterPageFile="" AutoEventWireup="true" CodeBehind="Lab4.aspx.cs" Inherits="" %>

CodeBehind="Lab4.aspx.cs" 是您在 C# 中指定代码隐藏文件的位置。

【讨论】:

【参考方案2】:

每个 aspx 都会有一个页面指令,它会告诉编译器在哪里寻找它的代码文件

<%@ Page CodeFile="Lab4.aspx.cs"  %>

documentation

【讨论】:

以上是关于aspx 页面无法与 aspx.cs 文件连接的主要内容,如果未能解决你的问题,请参考以下文章

无法访问 ASP.NET 上的 BLL 类

aspx.cs登陆页面弹出提醒窗口后按“确认”怎么跳转到首页?

使用 jquery ajax 在 aspx.cs 文件中调用 web 方法

用juery的ajax方法调用aspx.cs页面中的webmethod方法示例

新手求助,aspx.cs 里如何实现立即跳转网页

WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据(转)