Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnClick event firing twice 2

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

I have a page with fields that hooks up to a stored procedure.

[code--vb]

Protected Sub bAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bAdd.Click
If Not IsPostBack Then

Dim s As New clsADO

Dim SB As New System.Text.StringBuilder
SB.Append(s.UserID & ", '")
SB.Append(tFirst.Text & "', '")
SB.Append(tLast.Text & "', '")
SB.Append(tAdd1.Text & "', '")
SB.Append(tAdd2.Text & "', '")
SB.Append(tCity.Text & "', '")
SB.Append(ddState0.Text & "', '")
If Len(tZip.Text) < 10 Then
tZip.Text &= "00000"
End If
SB.Append(tZip.Text & "', '")
SB.Append(tEMail.Text & "', '")
SB.Append(tPhone1.Text & "', '")
SB.Append(tPhone2.Text & "'")
s.RunSQL("SP_InsertEMail " & SB.ToString)
lMsg.Text = "1 Record Added"
End If


End Sub
[/code]

[code--ASPX]
<asp:Button ID="bAdd" runat="server" BackColor="#CCFFFF" BorderColor="#CCFFFF"
OnClick="bAdd_Click" Text="Add" Width="92px" />

[/code]

It works, but it's inserting twice...my guess was on postback, which is why I tossed in the If Not IsPostBack statement. Once I did that, I get nothing.

I can easily add a boolean to make sure it only fires once, but what is the proper way?

I'd really like to learn this, and I get a curve with each new pitch.

Thanks,

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
the most common use of ispostback is in the page_load event. I have seen this happen when debugging <f5> using the casini VS web server (default). you can configure the website to run using IIS. click the button and check the database for duplicate records. if there are not duplicates the problem is casini. if there are duplicates the problem is your code.

if your code is the problem then you probably have the bAdd_Click handler assigned to the button's click event twice.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
The event "bAdd_Click" only fires in a postback, so there is no need to use "If Not IsPostBack Then ...".

The cause of the problem might be that you have event handler in both vb codes and declarative syntax. Try removing

OnClick="bAdd_Click"

from the declarative syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top