Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 21st, 2007, 11:06 AM
Registered User
 
Join Date: Sep 2007
Location: San Jose, CA, .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Button OnClick problem

I have experience with VB.Net on WinForms but new to web and ASP.Net programming. I am working thorugh Jacob Sanford's ASP.Net 2.0 Design book (excellent book BTW!).

My problem is that I put a textbox,button and label on a master page

<tr>
<td colspan="2" height="150" bgcolor="Gray" valign="middle" align="center">
Header Text:
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Update Header Text"
OnClick="Button1_Click"/>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>
</td>
</tr>

I then added the OnClick event code to the master page's code page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     Label1.Text = TextBox1.Text
     TextBox1.Text = ""
    End Sub

I test it with IE6. When the button is clicked, the OnClick event gets called twice. I know this because I put a break in debug mode and saw it.

What is my problem?

TIA,
John


  #2 (permalink)  
Old September 21st, 2007, 11:12 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Your click handler is getting wired to the click event twice:

Here in the "OnClick" attribute:
<asp:Button ID="Button1" runat="server" Text="Update Header Text" OnClick="Button1_Click"/>

and here with the "Handles" keyword:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Use one but not both. If you are using Visual Studio you should only be getting one or the other.

-Peter
  #3 (permalink)  
Old September 21st, 2007, 05:44 PM
Registered User
 
Join Date: Sep 2007
Location: San Jose, CA, .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Peter for that quick response. Indeed that was the problem. I used VS to create the "Handles..." code but manually entered it in the ASP section.

John



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error at button onClick asters ASP.NET 1.0 and 1.1 Basics 5 July 23rd, 2008 09:35 AM
Image button onclick msbsam ASP.NET 2.0 Basics 0 December 30th, 2006 04:47 AM
Change onclick of a button me_zeta HTML Code Clinic 1 November 20th, 2006 10:34 AM
Button onClick Apocolypse2005 Javascript 1 November 25th, 2005 09:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.