473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4721
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
8955
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
5121
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
5440
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
1711
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
5709
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
3387
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
8705
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
1542
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
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.