Strange button behavior!

eramgarden

Contributor
Joined
Mar 8, 2004
Messages
579
I have a page, 3 textboxes, one button. Focus in on the first button. I have a menu bar on top.

I type in the stuff, HIT ENTER but instead of going to the button's onclick event in the code-behind, it goes thru the menu bar code-behind and picks the page "home.aspx" and sends the user there without going thru the code !!!!

My other buttons are working fine when I hit "Enter".
What's the deal here???
 
I check it: This is what I have:

Code:
<asp:button id="btnSubmit" Text="Change Password" Runat="server"></asp:button>

Then in code behind , I have
Code:
 Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

I have nothing in my Page_Load routine

????
 
If I click on the button, it works.. it's when I hit "Enter" that it goes to the "home.aspx" page.

I set debug and it loads the menu bar (menu.ascx) and thinks "home.aspx" button is pressed !!
 
In your event properties what do you have for the button in question (we'll call it 'btnProblem' in the example below) on click event. I wouldn't expect the problem you described to have anything in the PageLoad event, I don't know where you got that.

btnProblem Events
----------------------------------
|OnClick | someprocedure_click |
----------------------------------

In fact cut and paste you whole code behind file.
 
All I'm saying is your calling the menu routine unknowingly, if you post the code behind we maybe able to help you figure out where you are doing it.
 
ah, sorry, ok, here it is:

This is the code behind

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
               

    End Sub

   


    Sub UpdateNewPwd(ByVal NewPwd As String)

        Dim cnn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnString"))
        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "WCUpdatePWD"



        cnn.Open()

        Try

            cmd.Parameters.Add(New SqlParameter("@NewPwd", NewPwd))
            cmd.Parameters.Add(New SqlParameter("@UserID", Session("Userid")))
            cmd.ExecuteNonQuery()

            lblError.Text = "You password has been changed."

        Catch eSQL As SqlException
            Dim objError As SqlError
            For Each objError In eSQL.Errors

                lblError.Text = "A Database error has occured.<br> " & objError.Message & " <br> Your data has not been updated."

            Next
            Exit Sub
        Finally
            cnn.Close()
        End Try
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click


        Dim strNewEncryption As String
        Dim EAPI As User  'Call EAPI
        EAPI = New User()

        Try


            If txtNewPassword.Text.Trim <> txtNewPasswordConfirm.Text.Trim Then
                lblError.Text = "Your 'New Password' and 'Confirm New Password' do NOT match! Please try again."


            Else
                ESPAPI.EncryptPassword(Session("userid"), txtNewPassword.Text.Trim, strNewEncryption)
                UpdateNewPwd(strNewEncryption)

            End If
        Catch ex As Exception
            lblError.Text = ex.Message.ToString

        End Try



    End Sub

  

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtPassword.Text = String.Empty
        txtNewPassword.Text = String.Empty
        txtNewPasswordConfirm.Text = String.Empty
    End Sub
End Class

This is the HTML

Code:
<%@ Register TagPrefix="uc1" TagName="SideMenu" Src="SideMenu.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="PasswordChange.aspx.vb" Inherits="ASPApp.PasswordChange"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>PasswordChange</title>
		<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
		<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<script language="JavaScript">
		 function setFocus()
            {
               document.Form1.txtPassword.focus();
              
               }
		</script>
	</HEAD>
	<body onload="setFocus()" MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<uc1:sidemenu id="SideMenu1" runat="server" BackColorPwd="Gray"></uc1:sidemenu>
			<table class="PWD2">
				<tr>
					<TD><asp:label id="lblError" runat="server" Font-Bold="True" ForeColor="Red"></asp:label></TD>
				</tr>
			</table>
			<table class="PWD">
				<TR>
				<TR>
					<TD style="WIDTH: 250px"><asp:label id="lblPassword" runat="server" cssClass="LoginBoxes">Old Password:</asp:label></TD>
					<TD><asp:textbox id="txtPassword" runat="server" TextMode="Password"></asp:textbox><asp:requiredfieldvalidator id="rfvPassword" runat="server" ErrorMessage="Old Password!" ControlToValidate="txtPassword" Display="Dynamic"></asp:requiredfieldvalidator></TD>
				</TR>
				<TR>
					<TD style="WIDTH: 250px"><asp:label id="Label1" runat="server" cssClass="LoginBoxes">New Password:</asp:label></TD>
					<TD><asp:textbox id="txtNewPassword" runat="server" TextMode="Password"></asp:textbox><asp:requiredfieldvalidator id="Requiredfieldvalidator2" runat="server" ErrorMessage="New Password!" ControlToValidate="txtNewPassword" Display="Dynamic"></asp:requiredfieldvalidator></TD>
				</TR>
				<TR>
					<TD style="WIDTH: 250px"><asp:label id="Label2" runat="server" cssClass="LoginBoxes">Confirm New Password:</asp:label></TD>
					<TD><asp:textbox id="txtNewPasswordConfirm" runat="server" TextMode="Password"></asp:textbox><asp:requiredfieldvalidator id="Requiredfieldvalidator3" runat="server" ErrorMessage="Confirm Password!" ControlToValidate="txtNewPasswordConfirm" Display="Dynamic"></asp:requiredfieldvalidator></TD>
				</TR>
				<tr>
					<td><asp:button id="btnSubmit" Text="Change Password" Runat="server"></asp:button></td>
					<td><asp:button id="Button1" CausesValidation=False Text="Refresh" Runat="server"></asp:button></td>
				</tr>
			</table>
		</form>
	</body>
</HTML>


I can post my menu code has well if u need to see that.
 
Last edited:
I was interested in see was the Page_Initialize procedure as this is probably where the error is.
 
I didnt change that procedure but here it is:

Code:
 Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

I have my menu.ascx in everypage. It gets called all the time, is that ok?? I dont have a "if not page.isPostback" in there...
 
I should've specified sorry...need the InitializeComponent() procedure, I implied that, but my fault for not being specific. Putting a control in every page is quite normal.
 
oh, Sorry, is this it? in global.asx

Code:
Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub
 
and yeah, when I click on the , for example, the refresh button on that page, I do see the "home" button on top kinda blink, it's all that button is being pressed...
 
I re-read your posts to see if I was missing something, and I was...it's really simple. When you're hitting ENTER you're causing a form submission (post back) and that's what causes the problem. Give me a minute while I research how to change that.
 
Last edited:
Okay, I can't figure it out, it's not something simple you can just turn off...there's probably a page directive or something that controls it. Sorry I wasted your time looking at that other stuff...now I'm curious too.
 
I did some testing and if you throw in a passowrd text box it will submit through the first button in finds. I don't know why. At least it is in my testing. Maybe you can try that. If it works put one off screen somewhere by using absolute positioning. That is crazy... I could've sworn I saw somewhere I way to make the form only submit if a certain button was pushed but I guess I'm mistaken... now we have two people looking for the same answer.
 
"throw in a passowrd text box" ..what do u mean, i have 3 textboxes now...

That link i sent u earlier..has something about creating a very small image button, have it size zero, then it would work... I think what you're saying is like that that link has...
 
Back
Top