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
Bette LamoreWeb Designer/Developer/GIS ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tapan PattanaikSenior EngineerCommented:
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

0
Bette LamoreWeb Designer/Developer/GIS ProfessionalAuthor Commented:
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:
error-button.JPGShould I change my Default.aspx.cs file as well? I included it in my original post.

Thanks for your assistance!
Bette
0
Miguel OzSoftware EngineerCommented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tapan PattanaikSenior EngineerCommented:
Hi Bette Lamore,

since, your button ID is "Button1" then  OnClick event should be "Button1_Click" in Default.aspx .

Please replace  OnClick="bnLogin_Click"  to   OnClick="Button1_Click"

code for the Default.aspx.cs  should be

private void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

code for the Default.aspx should be:

<%@ 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="Button1_Click" />
        &nbsp;</p>
</asp:Content>

Open in new window


code for the Default.aspx.cs should be:

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 Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bette LamoreWeb Designer/Developer/GIS ProfessionalAuthor Commented:
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.

First build error
build-error2.JPG
Any ideas?
thanks for your help :-)
0
Miguel OzSoftware EngineerCommented:
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.
0
Bette LamoreWeb Designer/Developer/GIS ProfessionalAuthor Commented:
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
set breakpointsran code and hit first breakpoint
clicked on run and hit first breakpointhit continue
hit continueclicked button
clicked on buttonhit continue again
clicked on continue again
0
Miguel OzSoftware EngineerCommented:
Please check that mark-up (aspx file) includes OnClick the event:
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="bnLogin_Click"/>

Open in new window

0
Bette LamoreWeb Designer/Developer/GIS ProfessionalAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.