Avatar of Bette Lamore
Bette LamoreFlag for United States of America

asked on 

private void bnLogin_Click(object sender, EventArgs e) does not work in Visual Studio 2010

The following is simple code yet this button click event does not redirect to the Home.aspx page. My button name is bnLogin so I have trouble shot errors in naming. Under web config I have:
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" defaultLanguage="c#" />
    </system.web>

</configuration>

Open in new window


My whole code for the Default.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace IasobergDataDrivenWebsite
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
       
        private void bnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
    }
}

Open in new window


And my code for the Default.aspx is
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IasobergDataDrivenWebsite.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <p>
        username
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
    </p>
    <p>
        password
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
    </p>
    <p>
        &nbsp;
        <asp:Button ID="bnLogin" runat="server" Text="Login" />
        &nbsp;</p>
</asp:Content>

Open in new window


Thanks for any assistance :-)
Bette
C#ASP.NET

Avatar of undefined
Last Comment
Bette Lamore
Avatar of Tapan Pattanaik
Tapan Pattanaik
Flag of India image

Hi Bette Lamore,

OnClick event for bnLogin is missing in  "Default.aspx " . Please add the OnClick event to btnLogin . Please check the below Code. OnClick="bnLogin_Click"


<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IasobergDataDrivenWebsite.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <p>
        username
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
    </p>
    <p>
        password
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
    </p>
    <p>
        &nbsp;
        <asp:Button ID="bnLogin" runat="server" Text="Login" OnClick="bnLogin_Click" />
        &nbsp;</p>
</asp:Content>

Open in new window

Avatar of Bette Lamore
Bette Lamore
Flag of United States of America image

ASKER

Hello Tapan
That code was contained in the Default.aspx.cs code yet I entered it in the Default.aspx as you suggested:
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IasobergDataDrivenWebsite.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <p>
        username
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
    </p>
    <p>
        password
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
    </p>
    <p>
        &nbsp;
        <asp:Button ID="Button1" runat="server" Text="Login" OnClick="bnLogin_Click" />
        &nbsp;</p>
</asp:Content>

Open in new window

and I got this error:
User generated imageShould I change my Default.aspx.cs file as well? I included it in my original post.

Thanks for your assistance!
Bette
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Yes, please change Default.aspx.cs file as shown below: (From private to protected - thus >NET can bind the event to the button)
 protected void bnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

Open in new window


Note: Both Tapan's change in the mark up  and code above are needed for the click to work.
ASKER CERTIFIED SOLUTION
Avatar of Tapan Pattanaik
Tapan Pattanaik
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bette Lamore
Bette Lamore
Flag of United States of America image

ASKER

Hi there Tapan
Sorry for delay -- I was out of town.
I had been troubleshooting so much that I failed to rename Button1 to my original name of bnLogin -- thanks for picking that up -- nevertheless, your code still does not work. I copied and pasted it into my Default.aspx and Default.aspx.cs pages (see pics below) and this time I got build errors.

User generated image
User generated image
Any ideas?
thanks for your help :-)
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

As per my previous post,  please change the access the modifier of the Button1_Click method from private to protected as described in this M$ link.
There is no need to change the event name as long as both mark up and code are using the same name.
Avatar of Bette Lamore
Bette Lamore
Flag of United States of America image

ASKER

Hello Miguel, I had just returned from a trip and did not see your post and responded to the last one by Tapan. I went back to my original code
(which had no build errors) and substituted your code of "protected" for private.
This time all appeared OK except my original problem of not redirecting to Home.aspx. I put 2 breakpoints -- the code ran through the first one yet stopped before it got to the second.
 
You can see I had your code change. The next images are the outputs -- perhaps that will help troubleshoot.
set breakpoints
User generated imageran code and hit first breakpoint
User generated imagehit continue
User generated imageclicked button
User generated imagehit continue again
User generated image
SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bette Lamore
Bette Lamore
Flag of United States of America image

ASKER

Hooray, Miguel -- with the one exception of the typo where you put Button1 instead of bnLogin, I added the bnLogin_Click to the Default.aspx page and it worked!!

The thing is, Tapan suggested the bnLogin_Click in the earlier post yet failed to tell me to replace the "Private" with the "Protected" -- your new suggestion.

I will have to split the solution between the two of you since Tapan got the bn_Login_Click first and you got the "Protected."

Thank you both VERY VERY much!!!!

Now I am going to see if I can attach this code to my database. If I cannot, look for my new question -- both of you are very helpful!!

Thanks again!!
Bette
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo