ASP.NET点击事件代码显示在ASPX页面中,而不是后面的代码 [英] Asp.net click event code appears in aspx page instead of code behind

查看:138
本文介绍了ASP.NET点击事件代码显示在ASPX页面中,而不是后面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮来定义其单击事件时,它将带我到自己的aspx页面的标签,而不是带我进入/aspx.cs页面的代码。这仅在主页上发生。

When i click on a button to define its click event, it takes me to the tags of my own aspx page instead of taking me to the code behind/aspx.cs page. This happens for home page only. the other page signup.aspx works fine and does as expected.

在过去的几个月中,与我一起进行的许多项目都发生了这种情况,而作为我的初学者,我对此没有太多想法。但是,是的,在VS 2010中创建此网站时,我已勾选 将代码放置在单独的文件中复选框。

This has happened for a number of projects with me in past few months and as i am a beginner, i don't have much idea about this. But yes i have tick marked "Place code in separate file" checkbox when creating this website in VS 2010.

在此先感谢您的帮助

<%@ Page Language="C#" AutoEventWireup="true"  %>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head id="Head1" runat="server">
            <title>Employee Login</title>
        <link href="css/Style.css" type="text/css" rel="Stylesheet" />
        </head>
        <body>
            <form id="form1" runat="server">
            <div>
                  <table align="center" class="loginBox">
                    <tr>
                        <td>Username:</td>
                        <td><asp:TextBox ID="txtUsername" runat="server" CssClass="username"></asp:TextBox></td>
                        <asp:RequiredFieldValidator runat="server" ID="rqUser" ValidationGroup="loginValidation" ControlToValidate="txtUsername" ErrorMessage="Username"></asp:RequiredFieldValidator>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="password"></asp:TextBox></td>
                        <asp:RequiredFieldValidator runat="server" ID="rqPass" ValidationGroup="loginValidation" ControlToValidate="txtPassword" ErrorMessage="Password"></asp:RequiredFieldValidator>
                    </tr>
                    <tr>
                        <td align="center" colspan="2"><asp:Button ID="btnLogin" Width="60" runat="server" 
                                Text="Login" CssClass="btnLogin" onclick="btnLogin_Click" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><span style="float:left;"><asp:HyperLink ID="HyperLink1" runat="server" Text="Forgot Password" NavigateUrl="~/forgotpassword.aspx" CssClass="regular_text"></asp:HyperLink></span>
                                        <span style="float:right;"><asp:HyperLink ID="HyperLink2" runat="server" Text="New User" NavigateUrl="~/signup.aspx" CssClass="regular_text"></asp:HyperLink></span>        
                        </td>
                    </tr>
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You must enter following" DisplayMode="BulletList" EnableClientScript="true" ForeColor="White" />
                </table>
            </div>
            </form>
        </body>
        </html>


    Code behind:

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

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }


推荐答案

在.aspx文件的标头。应该有一个名为CodeBehind的属性,该属性引用您的代码文件。就像

Look in the header of the .aspx file. There should be a property named CodeBehind which references to your code file. Is something as

CodeBehind="yourfile.aspx.cs"

如果不存在此属性,则C#代码将放置在aspx的脚本标记内。如果存在此属性,请引用后面带有代码的cs文件。创建文件时,可能没有勾选将代码放置在单独的文件中复选框(或类似的复选框)。

If this property is not present, the C# code is placed inside script tags in your aspx. If this property is present, references the cs file with the code behind. Probably, when you created the file, you did not tick the checkbox "Place code in a separate file" (or similar).

如果要将代码保存在单独的文件夹中,则可以使用代码创建一个新的aspx.cs文件(更好地从解决方案中的其他文件复制并粘贴) ),并添加引用文件的属性CodeBegind。

If you want to have the code in a separate folder, you can create a new aspx.cs file with the code (better copy and paste from other file in your solution), and add the property CodeBegind referencing the file.

这篇关于ASP.NET点击事件代码显示在ASPX页面中,而不是后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆