Button Routines being called twice

Discussion in 'ASP .Net' started by mosscliffe, May 24, 2006.

  1. mosscliffe

    mosscliffe Guest

    I am sorry but I am all very new and slow at understanding all this
    ASP.NET2.

    I found some code which showed how to page with a repeater. All very
    excited as I had been looking for this all day. It worked wonderfully,
    but it was in C# So I converted it to VB with

    http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

    It got upset about private and so I changed those to protected

    My problem is the NEXT Button subroutine gets called twice, but the
    PREV one only got called once.

    After lots of hair pulling, I eventually worked out that it was because
    the PREV button did not have the Handles cmdPrev.Click. I added that
    to the PREV Button and now that executes twice. I do not fully
    understand this Handles reference, but it is always there if I set up a
    default event for a Button.

    I don't think it is a postback problem, because the page is only
    loading once and I have commented out all other code.

    OK so why is / are the Button routines being executed twice.

    The code is as follows.

    Imports System.data
    Partial Class timrepeat
    Inherits System.Web.UI.Page


    Public Property CurrentPage() As Integer
    Get
    Dim cpg As Object = Me.ViewState("_CurrentPage")
    If cpg Is Nothing Then
    Return 0
    Else
    Return CType(cpg, Integer)
    End If
    End Get
    Set(ByVal value As Integer)
    Me.ViewState("_CurrentPage") = value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    'ItemsGet()
    lbxDebug.Items.Insert(0, "PageLoad:
    --->>>>>>>>>>>>>>>>>>>>>>>>>--- Curr Page:" & CurrentPage.ToString)

    End Sub

    Private Sub ItemsGet()
    Dim Items As DataSet = New DataSet
    Items.ReadXml(MapPath("Items.xml"))
    Dim objPds As PagedDataSource = New PagedDataSource
    objPds.DataSource = Items.Tables(0).DefaultView
    objPds.AllowPaging = True
    objPds.PageSize = 1
    objPds.CurrentPageIndex = CurrentPage
    lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString + "
    of " + objPds.PageCount.ToString
    cmdPrev.Enabled = Not objPds.IsFirstPage
    cmdNext.Enabled = Not objPds.IsLastPage
    repeaterItems.DataSource = objPds
    repeaterItems.DataBind()
    End Sub

    Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cmdNext.Click
    lbxDebug.Items.Insert(0, "NEXTBEFORE:CurrentPage= " &
    CurrentPage.ToString)
    CurrentPage += 1
    lbxDebug.Items.Insert(0, "NEXTAFTER:CurrentPage= " &
    CurrentPage.ToString)
    'ItemsGet()
    End Sub

    Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cmdPrev.Click
    lbxDebug.Items.Insert(0, "PREVBEFORE:CurrentPage= " &
    CurrentPage.ToString)
    CurrentPage -= 1
    lbxDebug.Items.Insert(0, "PREVAFTER:CurrentPage= " &
    CurrentPage.ToString)
    'ItemsGet()
    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button1.Click

    End Sub
    End Class

    The aspx page is

    <%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="timrepeat.aspx.vb" Inherits="timrepeat" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <%--<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>
    --%>
    <html>
    <head>
    <title>TestRepeater</title>
    </head>
    <body>
    <form id="frmTest" method="post" runat="server">
    <table width="100%" border="0">
    <tr>
    <td>&nbsp;&nbsp;Repeater control with Paging functionality</td>
    </tr>
    <tr>
    <td>&nbsp;&nbsp;<asp:label id="lblCurrentPage"
    runat="server"></asp:label></td>
    </tr>
    <tr>
    <td>&nbsp;&nbsp;<asp:button id="cmdPrev" runat="server"
    text="PREV" onclick="cmdPrev_Click"></asp:button>
    &nbsp;<asp:button id="cmdNext" runat="server" text="NEXT"
    onclick="cmdNext_Click"></asp:button></td>
    </tr>
    </table>
    <table border="1">
    <asp:repeater id="repeaterItems" runat="server">
    <itemtemplate>
    <tr>
    <td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
    "ItemName") %></b></td>
    <td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
    "ItemDescription") %></b></td>
    <td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
    "ItemPrice") %></b></td>
    <td>&nbsp;&nbsp;<b><%# DataBinder.Eval(Container.DataItem,
    "ItemInStock") %></b></td>
    </tr>
    </itemtemplate>
    </asp:repeater>
    </table>
    &nbsp;
    <asp:ListBox ID="lbxDebug" runat="server"
    Rows="10"></asp:ListBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
    </body>
    </html>
     
    mosscliffe, May 24, 2006
    #1
    1. Advertisements

  2. mosscliffe

    raghu1 Guest

    I had a similar problem with .net 1.1 and c#.
    Figured out that AutoEventWireup s/be false. I am not sure how this
    will be in 2.0 and vb.
    Hope this helps.
     
    raghu1, May 24, 2006
    #2
    1. Advertisements

  3. mosscliffe

    mosscliffe Guest

    I can not see where I could try autoeventwireup. The button does not
    seem to have that property.
     
    mosscliffe, May 24, 2006
    #3
  4. mosscliffe

    raghu1 Guest

    This is on the Page (.aspx) and not button, usually the 1st line
    Here is my code snippet:
    <%@ Page language="c#" Codebehind="searchResults.aspx.cs"
    AutoEventWireup="false" Inherits="searchResults" %>
     
    raghu1, May 24, 2006
    #4
  5. mosscliffe

    mosscliffe Guest

    It just shows my lack of knowledge and understanding - I already had
    that in my original page declaration, see original post.

    Thanks for your input, but it looks like back to the drawing board for
    a solution.
     
    mosscliffe, May 24, 2006
    #5
  6. mosscliffe

    raghu1 Guest

    Yes, I missed this.
    In my case, I inserted a record into a table on postBack with
    date-time stamp, control name just to figure out what is happening.
     
    raghu1, May 24, 2006
    #6
  7. mosscliffe

    mosscliffe Guest

    There is some underlying events/logic, which I am not understanding, as
    I said originally, if I remove the handles element from the Button
    Routines they work. Now I can not grasp what the 'Handles
    button1.click' do/mean.

    If anyone can enlighten me - I would be most grateful.
     
    mosscliffe, May 24, 2006
    #7
  8. mosscliffe

    Guest Guest

    "Handles" is VB.NET syntax for "Make this handle the XXX_whatever event"
    (Click, etc.)
    It does the same thing that AutoEventWireup does at the page level, hence
    you could have your events firing twice for the control.
    Peter
     
    Guest, May 25, 2006
    #8
  9. mosscliffe

    mosscliffe Guest

    Thank you. I will get there - eventually - he says - through clenched
    teeth and little conviction !
     
    mosscliffe, May 25, 2006
    #9
  10. mosscliffe

    Ciaran Guest

    The handles makes VB make a call to AddHandler which registers the a
    delegate for the methods on the click event for the button. This will make
    the button click call once.
    In the aspx page however, you have the OnClick attributes of the buttons
    pointing to the methods, this will also make a call to addhandler so you
    will have 2 delegates for the same method on the same event. Therefore it
    will run twice.
    Remove one or the other, you dont need both,


    HTH

    Ciaran
     
    Ciaran, Jun 18, 2006
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.