Solved

Asp.net 2.0 button click subroutine executing twice

Posted on 2007-04-11
4
622 Views
Last Modified: 2013-11-26
I have a simple Asp.Net 20.0 page done in Visual Studio 2005 using VB.Net.  On this page, there is a button called btnCreateReport.  When I press this button, what is supposed to occur is the firing of an objectdatasource (odsCreateReport) which fires a Sql Server 2005 stored procedure that creates a new record in a Reports table.  What happens is, when the Create Report button is pressed it calls the associated subroutine (btnCreateReport_Click) twice.  It should only be running once.  I've run it in debug (something I'm new to) and put a break point on the opening line of the sub:

Public Sub btnCreateReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click

It stops on this line, but when I hit F5 to continue it breaks at that line again.  Hitting F5 to continue runs the subroutine again, thereby adding two Report records.  Obviously, this is undesirable.  Why is this sub. firing twice?

Here's the code, watered down for brevity:

Report_master.aspx ---

Button code:
<asp:Button ID="btnCreateReport" runat="server" Text="Create Report" OnClick="btnCreateReport_Click" />

objectdatasource code:

<asp:ObjectDataSource ID="odsCreateReport" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetPrevReportByEmployerCode"
        TypeName="pf120TableAdapters.ReportTableAdapter" InsertMethod="InsertNewReport" >
        <SelectParameters>
            <asp:SessionParameter Name="employerCode" SessionField="employerCode" Type="String" />
        </SelectParameters>
        <InsertParameters>
            <asp:SessionParameter Name="reportDate" SessionField="reportDate" Type="DateTime" />
            <asp:SessionParameter Name="employerCode" SessionField="employerCode" Type="String" />
            <asp:Parameter Direction="InputOutput" Name="reportid" Type="Object" />
        </InsertParameters>
    </asp:ObjectDataSource>

Report_master.aspx.vb:

Public Sub btnCreateReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click

        Dim dvwReport As DataView = odsCreateReport.Select
        Dim intReportCount As Integer = dvwReport.Count

        If dvwReport.Count > 0 Then
                odsCreateReport.Insert()
       End If

End Sub


The odsCreateReport.Select simply uses a stored procedure to grab the previous report to retrieve its Report ID for later use.  I haven't built that part of the code yet.

I would appreciate any ideas.  Thanks.

Bill



0
Comment
Question by:thewyzzard
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
dmagliola earned 500 total points
ID: 18892060
This normally happens (events firing twice) when events are wired to the control twice.
Generally you do this without noticing it.

For example, you are doing:

OnClick="btnCreateReport_Click" (on the control declaration)
Handles btnCreateReport.Click (on the event method declaration)

Try removing the OnClick="btnCreateReport_Click" from the control declaration.
0
 

Author Comment

by:thewyzzard
ID: 18892099
Thanks DMag.  I've always done it that way.  Was I just getting lucky and dodging bullets?  I've moved over to 2.0 from 1.1 recently.  Does this behave differently in 2.0?  

Just curious.  

Thanks for the quick reply.

Peace,
Bill
0
 
LVL 3

Expert Comment

by:dmagliola
ID: 18892154
Well...
I'm not very clear on the answer to that one, I haven't experimented that much with that (I never used the OnClick syntax) but i'll tell you one thing that should point you in the right direction.

I learned about this trick because I heard of someone that was getting this behaviour in C#, but not in VB.
Turns out VB normally "realizes" you're double-wiring the event, and supresses the extra call, while C# assumes you know what you're doing and lets you do it.

The reason for this is historical, Microsoft has always considered VB programmers needed to be spoon-fed, while C programmers were supposed to be "smart people that could see the matrix, not like the other guys that didn't even get pointers".
I always found that offensive, but truth is, if you try to use C#, you'll see how much friendlier the VB environment is because "we're not that smart". Point for VB, again :-)

It's possible they've suppressed this "auto-removal-of-double-wiring" in 2.0, for some good or bad reason, but I wouldn't know for sure.
Bottom line, don't use the OnClick syntax. It's kind of a hack designed so you could have <script runat="server"> blocks, which aren't a good idea anyway. The correct "object oriented" way is the "handles" keyword.
0
 

Author Comment

by:thewyzzard
ID: 18892197
Point well taken.  Thanks for the info.  I love VB, regardless of what the other side says.

Peace and thanks again.

Bill
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
error on the asp.net page 1 33
Get id from json Data with NewtonSoft 3 31
Class object 2 26
Write to file when application closes (by logoff) - VB.NET 1 13
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now