473,407 Members | 2,314 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,407 software developers and data experts.

Button ServerClick event firing twice in ASP.NET page

Oz
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 selected, it causes the ServerClick event to be called twice.

Here's the code: You'll see that there are two grids on the page, and that
the function SetupDataGrid populates data to the two grid. There are also
three buttons on the page. All of the them fire events twice.
An example of one of these buttons is:

<TD vAlign="middle" align="center">
<P>
<BUTTON id="cmdDelete" runat="server" class="TaskFrameButtons"
type="submit">
<TABLE class="TaskFrameButtonsNoBorder" id="Table5" cellSpacing="0"
cellPadding="0" width="30"
border="0">
<TR>
<TD align="center" width="28"><IMG
src="images/butDelete.gif">&nbsp;</TD>
</TR>
</TABLE>
</BUTTON>
</P>
</TD>

and the ASP.NET code behind in vb is below:

Public Class Cashier
Inherits System.Web.UI.Page

Enum BUTTON_CLICKED
ADD_SELECTED = 1
ADD_ALL
DELETE_SELECTED
End Enum
Enum GRID_FIELDS
CHECKBOX
TITLE
SERIAL_NO
INVENTORY_ID
End Enum
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents frmSearch As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents cmbSearchType As
System.Web.UI.WebControls.DropDownList
Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox
Protected WithEvents dgAvailable As System.Web.UI.WebControls.DataGrid
Protected WithEvents dgSelected As System.Web.UI.WebControls.DataGrid
Protected WithEvents cmdAddSelected As
System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents cmdAddAll As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents cmdDelete As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents txtShowSell As System.Web.UI.WebControls.TextBox
Protected WithEvents lblMsg As System.Web.UI.WebControls.Label
Protected WithEvents cmdSell As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents litSell As System.Web.UI.WebControls.Literal
Protected WithEvents cmdSearch As System.Web.UI.HtmlControls.HtmlButton
Protected WithEvents lblTotalValue As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim isin As New
ISInventory.BusinessServices(Application("inventor y_ConnectionString"))
Dim li As ListItem
Dim gd As New Guid(CStr(Session("UserID")))

Try

If IsPostBack Then

' Need this code in here to avoid the situation where a
button is clicked and then the
' grid Available reloads the data. If this happens, it
'forgets' which checkboxes where
' selected, meaning that cmdAddSelected event handler below
never inserts any inventory item
' to the database for a specific user. Therefore, if either
of these two buttons are clicked, want
' to avoid refreshing the datagrid, which is what the code
below does.
If Request.Form("__EVENTTARGET") = "cmdAddSelected" Or
Request.Form("__EVENTTARGET") = "cmdAddAll" Or Request.Form("__EVENTTARGET")
= "cmdDelete" Then
Exit Sub
End If

' Retrieve the items to sell from the temporary table in the
database
' When each user selects items to sell, they are put into a
temporary table
' until the user selects to sell them. Then they are
removed from the temporary
' table but are sold by the app (i.e. IsSold bit field is
set).
SetupDataGrids(isin, gd)

Else
If Not isin.Security.IsAuthentificated(Session("UserID"),
Session("LOGON_TYPE")) Then

Server.Transfer("logon.aspx")
End If

cmbSearchType.Items.Clear()
li = New ListItem("Product", 1)
cmbSearchType.Items.Add(li)
li = New ListItem("Part #", 2)
cmbSearchType.Items.Add(li)
li = New ListItem("SerialNo", 3)
cmbSearchType.Items.Add(li)

' Retrieve the items to sell from the temporary table in the
database
' When each user selects items to sell, they are put into a
temporary table
' until the user selects to sell them. Then they are
removed from the temporary
' table but are sold by the app (i.e. IsSold bit field is
set).
SetupDataGrids(isin, gd, True)
End If
Catch ex As Exception
lblMsg.CssClass = "Failure"
lblMsg.Text = ex.Message
End Try
End Sub

Private Sub dgAvailable_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles
dgAvailable.PageIndexChanged

With dgAvailable
.CurrentPageIndex = e.NewPageIndex

' If the grid is sorted, reapply the sort expression here.
.DataBind()
End With

End Sub
Private Sub dgSelected_PageIndexChanged(ByVal source As System.Object,
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args)
With dgSelected
.CurrentPageIndex = e.NewPageIndex

' If the grid is sorted, reapply the sort expression here.
.DataBind()
End With

End Sub
Private Sub cmdAddSelected_ServerClick(ByVal sender As Object, ByVal e
As System.EventArgs) Handles cmdAddSelected.ServerClick
TakeAction(cmdAddSelected)
End Sub

Private Sub cmdAddAll_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdAddAll.ServerClick
TakeAction(cmdAddAll)

End Sub

Private Sub cmdDelete_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdDelete.ServerClick
TakeAction(cmdDelete)

End Sub

' decide whether to add or remove items from the
' different grid
Public Sub TakeAction(ByVal cmd As HtmlButton)
Dim dg As DataGrid
Dim item As DataGridItem
Dim butClicked As BUTTON_CLICKED
Dim isin As New
ISInventory.BusinessServices(Application("inventor y_ConnectionString"))
Dim gdUser As New Guid(CStr(Session("UserID")))
Select Case cmd.ID
Case "cmdAddSelected"
dg = dgAvailable
butClicked = BUTTON_CLICKED.ADD_SELECTED
Case "cmdAddAll"
dg = dgAvailable
butClicked = BUTTON_CLICKED.ADD_ALL
Case "cmdDelete"
dg = dgSelected
butClicked = BUTTON_CLICKED.DELETE_SELECTED
End Select

' If either the Add single or delete single buttons have been
selected
If (butClicked = BUTTON_CLICKED.ADD_SELECTED Or butClicked =
BUTTON_CLICKED.DELETE_SELECTED) Then
Dim c As CheckBox
Dim gdItem As Guid, gdLastItem As Guid
For Each item In dg.Items

c = item.Cells(GRID_FIELDS.CHECKBOX).Controls(1) ' Can't use
control 0 as this is the literal control (see MSDN.NET)
If c.Checked Then

gdItem = New
Guid(item.Cells(GRID_FIELDS.INVENTORY_ID).Text)

' There is a bug, which causes the code below to run
twice
' Therefore, this code solves it
If Not gdItem.Equals(gdLastItem) Then
Select Case butClicked
Case BUTTON_CLICKED.ADD_SELECTED
isin.AddItemToSell(gdUser, gdItem, 0)
Case BUTTON_CLICKED.DELETE_SELECTED
isin.RemoveItemToSell(gdUser, New
Guid(item.Cells(GRID_FIELDS.INVENTORY_ID).Text))
End Select

gdLastItem = gdItem
End If
End If
Next
ElseIf butClicked = BUTTON_CLICKED.ADD_ALL Then
' If either the Add all button has been selected

For Each item In dg.Items

isin.AddItemToSell(gdUser, New
Guid(item.Cells(GRID_FIELDS.INVENTORY_ID).Text), 0)
Next

End If

Dim gd As New Guid(CStr(Session("UserID")))

' Retrieve the items to sell from the temporary table in the
database
' When each user selects items to sell, they are put into a
temporary table
' until the user selects to sell them. Then they are removed from
the temporary
' table but are sold by the app (i.e. IsSold bit field is set).
SetupDataGrids(isin, gd)
End Sub

Private Sub SetupDataGrids(ByVal isin As ISInventory.BusinessServices,
ByVal gdUser As Guid, Optional ByVal bInitialLoad As Boolean = False)
' This code adds an empty row to the DataGrid
' so that it doesn't disappear when it is empty
Dim dtAvailableItems As DataTable, dtEmptyItems As DataTable
Dim dtItemsToSell As DataTable
Dim col As DataColumn = New DataColumn
Dim i As Integer
Dim bDataSetReturnedEmpty As Boolean

Try
' Check if the user is searching for any values. If so, pass
search
' parameters to the search method, otherwise, get all values.

' Don't load anything when the page is loaded for the first time
With dgAvailable
If Not bInitialLoad Then
If txtSearch.Text <> vbNullString And
IsNumeric(cmbSearchType.SelectedValue) Then
dtAvailableItems =
isin.SearchInventoryItemsNotForSaleByUser(New Guid(CStr(Session("UserID"))),
txtSearch.Text, CInt(cmbSearchType.SelectedValue))

' Cannot put this statement directly in the if
statement below as, in the case where bInitialLoad is True, a runtime error
' is returned as dtAvailableItems will be Nothing!
So need this intermediate variable bDataSetReturnedEmpty to get this value
If dtAvailableItems Is Nothing Then
bDataSetReturnedEmpty = True
ElseIf (dtAvailableItems.Rows.Count = 0) Then
bDataSetReturnedEmpty = True
End If
Else
bDataSetReturnedEmpty = True
End If
End If
If bInitialLoad Or bDataSetReturnedEmpty Then

' There are no avaiable rows
dtEmptyItems = New DataTable("Inventory")
col.DataType = System.Type.GetType("System.String")
col.ColumnName = "Title"
dtEmptyItems.Columns.Add(col)

col = New DataColumn
col.DataType = System.Type.GetType("System.Guid")
col.ColumnName = "InventoryID"
dtEmptyItems.Columns.Add(col)

col = New DataColumn
col.DataType = System.Type.GetType("System.String")
col.ColumnName = "SerialNo"
dtEmptyItems.Columns.Add(col)
For i = 1 To 10
dtEmptyItems.Rows.Add(dtEmptyItems.NewRow())
Next

With dgAvailable
.CellPadding = 0
.CellSpacing = 1
.Columns(GRID_FIELDS.CHECKBOX).Visible = False
.DataSource = dtEmptyItems
.DataBind()
End With
Else

' There are avaiable rows
.DataSource = dtAvailableItems
.DataBind()

.CellPadding = 0
.CellSpacing = 0
.Columns(GRID_FIELDS.CHECKBOX).Visible = True

End If
End With

With dgSelected
Dim dgSelectedItemFirst As DataGridItem
dtItemsToSell = isin.GetItemsToSell(gdUser)

' Again, check for empty datatable and take action

.DataSource = dtItemsToSell
.DataBind()

' Set the total value lbl
lblTotalValue.Text =
Format(isin.GetItemsToSellValue(gdUser), "#,##0.00")

' If the first item text is blank, we know that this is
because the
' is an empty datatable. Therefore, disable the checkbox, so
that user cannot attempt add it to
' the dgSelected datagrid. Extra row has been inserted by
the code in DBData Execute method
' (search for "EXTRA BLANK ROW INSERT" in DBData to see
this code, which is necessary to stop the
' datagrid disappearing on the webpage - which it does by
default if there are no records)
'dgSelectedItemFirst = .Items(0)
'If dgSelectedItemFirst.Cells(GRID_FIELDS.TITLE).Text =
"&nbsp;" Then
'
dgSelectedItemFirst.Cells(GRID_FIELDS.CHECKBOX).Co ntrols(GRID_FIELDS.CHECKBO
X).Visible = False

'Else
'
dgSelectedItemFirst.Cells(GRID_FIELDS.CHECKBOX).Co ntrols(GRID_FIELDS.CHECKBO
X).Visible = True
'End If
End With
Catch ex As Exception
lblMsg.CssClass = "Failure"
lblMsg.Text = ex.Message

End Try
End Sub

Private Sub cmdSell_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdSell.ServerClick
Dim isin As New
ISInventory.BusinessServices(Application("inventor y_ConnectionString"))
Dim dtItemsToSell As DataTable
Dim gdUser As New Guid(CStr(Session("UserID")))
Dim gdSaleDisplay As Guid
Dim row As DataRow

gdSaleDisplay = System.Guid.NewGuid()

If dgSelected.Items.Count = 0 Then
lblMsg.CssClass = "Failure"
lblMsg.Text = "There are no items to sell!! <br>Search for and
add items to sell before selecting the sell button"
Exit Sub
End If
Try

isin.UpdateItemsToSellForDisplay(gdUser, gdSaleDisplay)
Response.Redirect("cashier2.aspx?id=" &
gdSaleDisplay.ToString())
Catch ex As Exception
lblMsg.CssClass = "Failure"
lblMsg.Text = ex.Message

End Try
End Sub

End Class
This does not happen every time the button is click, but just those times
when the page is loaded from a user navigating from another page. If the
page is just refreshed and the button clicked
Nov 18 '05 #1
0 5700

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

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
4
by: Chris Lane | last post by:
Hi, I have an HTML RESET button and Web Control Button that posts back to the server. Problem is the reset button doesn't work after the postback occurs. Any ideas or suggestions as to why this...
7
by: Shane Bishop | last post by:
I've been fighting with the Page_Load event firing twice. I looked through this user group and saw several other people having similar problems. There were various reasons for it:...
4
by: Seraph | last post by:
Again, I'm rather new here, so if I fail to follow any etiquette, please forgive me and let me know what I've done wrong, but I think this might interest quite a few people. One of my colleaques...
3
by: Patrick Olurotimi Ige | last post by:
Can i have a Button in a Datalist? The button is for posting a form to the the Database FOr example:- <ASP:DataList id="heading0" RepeatLayout="Flow" RepeatDirection="Horizontal" ...
6
by: schleichv | last post by:
Hello, I am attempting to get the ServerClick event to fire on a dynamically generated HtmlInputImage and having no luck at all. I've looked over many items on the forums but none seem to cover...
14
by: TS | last post by:
I have this custom data list control and i override the onItemDatabound event. After upgrading to vs 2005, this event is not always getting called, though it does at other times. No changes were...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
3
by: Jay | last post by:
I am on the 2.0 framework and have run the c:\windows\microsoft.net \framework\v1.1.4322\aspnet_regiis.exe -c and had no success. About half of the buttons on my webforms are firing and the other...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.