Solved

Asp.net 2.0 button click subroutine executing twice

Posted on 2007-04-11
4
621 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

763 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

9 Experts available now in Live!

Get 1:1 Help Now