Solved

Asp.net 2.0 button click subroutine executing twice

Posted on 2007-04-11
4
623 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 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