RKiyer
RKiyer

Reputation: 21

asp button click event

I am using C#.net for creating an aspx page (Visual Studio 2010). I have copied most of the template code from online ( HTML ) but when I drop a button of asp and double click it, it doesn't redirect to the code behind page but, instead creates "onclick=btnSubmit_Click" event in the source code.

<asp:Button ID="btnSubmit" runat="server" Text="Submit" Height="30px" 
Width="100px" **onclick="btnSubmit_Click"** />

Ideally it should go to the code behind page and allow event handling stuffs, I have no idea why this is happening.

My question is:
1. What do I do in order to get redirected like normal asp pages in Visual Studio
2. If possible, can someone explain what am I doing wrong here?

**I did get a way around it by using javascript

<script type="text/javascript" runat="server">
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    txtName.Text="btn submit clicked"
End Sub

Upvotes: 1

Views: 1227

Answers (2)

hdkhardik
hdkhardik

Reputation: 662

please make sure that your <%Page directive has mentioned CodeBehind file.

i.e

something like this

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/example.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Example.default" %>

this will be on the top of your aspx page

Upvotes: 1

Jayesh Goyani
Jayesh Goyani

Reputation: 11154

Please try with the below methods.

Method 1:

Based on what I found here: http://forums.asp.net/t/1229894.aspx/1

  1. Right click on your solution explorer.
  2. Add New Item -> Class File.
  3. Name the file as the name of your aspx eg: Default.aspx.cs
  4. When it asks you the file should be in app_code click "no".
  5. In your aspx in page attribute add

    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"

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

  6. Similarly in your class file that you just added remove everything. Your class should look like this:

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

    }
    

    }

Method 2: while adding new page please make sure the place code in separate file box is checked

enter image description here

Upvotes: 0

Related Questions