By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

Button ServerClick event firing twice in ASP.NET page

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.