Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Asp.net 2.0 button click subroutine executing twice

Posted on 2007-04-11
4
Medium Priority
?
631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
dmagliola earned 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
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.

660 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