Link to home
Start Free TrialLog in
Avatar of thewyzzard
thewyzzardFlag for United States of America

asked on

Asp.net 2.0 button click subroutine executing twice

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



ASKER CERTIFIED SOLUTION
Avatar of dmagliola
dmagliola

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thewyzzard

ASKER

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
Avatar of dmagliola
dmagliola

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.
Point well taken.  Thanks for the info.  I love VB, regardless of what the other side says.

Peace and thanks again.

Bill