473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Button Routines being called twice

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/uti...sharptovb.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>

May 24 '06 #1
9 4713
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.

May 24 '06 #2
I can not see where I could try autoeventwireup. The button does not
seem to have that property.

May 24 '06 #3
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" %>

May 24 '06 #4
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.

May 24 '06 #5
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.

May 24 '06 #6
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.

May 24 '06 #7
"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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"mosscliffe" wrote:
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.

May 25 '06 #8
Thank you. I will get there - eventually - he says - through clenched
teeth and little conviction !

May 25 '06 #9
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

"mosscliffe" <pa***********@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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/uti...sharptovb.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>

Jun 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Logger | last post by:
Help, I’m trying to implement a confirm button on an asp.net page. I have it attached to a asp:button control. In the button1 click event I call the CreateConfirmBox subroutine. The Box comes...
3
by: Jason Kyle Baginski | last post by:
Here's a little test app to demonstrate a problem I'm having. It creates four buttons, each one with the different FlatStyle types available. Three of them behave exactly the same way(and the...
5
by: Nikhil Patel | last post by:
Hi all, I have a strange problem. I have a button called btnSubmit on a .aspx page. I have written btnSubmit_Click method in C# to handle the button's click event. In the .aspx file, the onclick...
1
by: mike parr | last post by:
I have an button which calls its Click procedure twice when clicked. This is my code : public void btnAddToCartPersonal_Click(object sender, System.Web.UI.ImageClickEventArgs e) { //validate...
0
by: Oz | last post by:
Hi Using VS.NET 2003, Windows XP SP1, We have a page which has been developed using ASP.NET. On it, is a button which when clicked is supposed to add some data to a table. When the button is...
2
by: Samy | last post by:
Hi There, I have a user control with buttons on it which I use on a aspx page (parent page). On a button click, a modal dialog(aspx page) opens up and the user enters some info in the modal dialog...
3
by: Imran Aziz | last post by:
Hello All, I have a search text and button that post data and my button handler filters the repeater control. However when the button is clicked the first time. The page_load event is being called...
0
by: Diffident | last post by:
Hello All, I have an asp:button and I have tied an eventhandler to this button's click event which means that the eventhandler should be executed everytime I click the button. But to my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.