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

淺唱寂寞╮ 提交于 2020-01-26 04:04:34

问题


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

        }
    }

回答1:


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.




回答2:


At the top of your .aspx page - you'll see a line of code like so (or something similar):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs"
Inherits="yourapp.default"%>

The CodeBehind property links to the .CS file (C# code file) of the .aspx (asp markup / designer) page

the Inhertis property links to "namespace.partial" class of that .CS file

The .CS File should then look something like this:

...
using System.Data.SqlClient;
using System.Net;

namespace yourapp
{
    public partial class Default : System.Web.UI.Page
    {

...

If you don't have said properties in your ASPX file - Add them

If you dont have a namespace directive in your C# code behind file - add it (Using the name you gave your project of course(check your other files that work how it looks)) and encapsulate everything under namespace (i.e add {} around everyting under the namespace clause)




回答3:


If its a HTML control, the click event will be define in aspx page by double clicking it. If its an asp control, click event will be defined in code page by double clicking it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="YourCodePage.aspx.cs"
    Inherits="Default2" %>


来源:https://stackoverflow.com/questions/24161119/asp-net-click-event-code-appears-in-aspx-page-instead-of-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!