Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

c# - Asp.net click event code appears in aspx page instead of code behind

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.

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.

Thanks in advance for your help.

Edit: (Added code)

<%@ 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)
        {

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...