DataGrid EditCommandColumn not firing events properly.

R

Rick

Hello all, I hope all is well with you.

I am having a seriously difficult time with this problem. Allow me to
set
up the problem.

I have a System.Web.UI.Page with the following controls (watch the
layout,
some have child controls):

1. DataGrid (dgContracts) - Lists current contracts.
1.1. ButtonColumn (CommandName SelectContract) - Selects a contract
to
use.
2. Repeater (rptrJobActivities) - Lists all Job Activities for a
Contract/Job when a user selects a contract from

dgContracts. Each RepeaterItem represents a single JobActivity.
2.1. DropDownList (ddlInstallers <asp:DropDownList
ID="ddlInstallerWorkers" DataSource='<%#

InstallerWorkers()%>' DataTextField="FullName"
DataValueField="WorkerId"
Runat="server" />) Lists all available

Installers (people)
2.2. Button (btnAddInstaller <asp:Button CssClass="submit"
ID="btnAddInstaller" Text="Add"

OnClick="btnAddInstaller_Click"

Runat="server" />) Adds the selected installer in ddlInstallers to the
JobActivityInstallers collection.
2.3. DataGrid (dgInstallers <asp:DataGrid
ID="dgJobActivityInstallers"
AutoGenerateColumns="False"

Width="75%" Runat="server">)

Displays the installers that are added to the job activity
(AddHandlers to
EditCommand, CancelCommand and

UpdateCommand in

rptrJobActivities_ItemCreated - NOTE: Originally I had these assigned
in
the <asp:DataGrid

OnEditCommand="dgInstallers_EditCommand"
OnCancelCommand="dgInstallers_CancelCommand"

OnUpdateCommand="dgInstallers_UpdateCommand"/> until this problem came
up)
2.3.1. EditCommandColumn (<asp:EditCommandColumn
ButtonType="LinkButton"
CancelText="Cancel"

EditText="Edit"

UpdateText="Update"/>)


A user will load this page, dgContracts is DataBind only on Not
IsPostback,
select a contract from dgContracts, by

clicking the ButtonCol The page posts, dgContracts_ItemCommand fires -
rpterJobActivities is DataBind, and

rptrJobActivities is populated with all the Job Activities associated
with
the selected contract. Everytime

rptrJobActivities_ItemCreated is fired dgInstallers is DataBind and
AddHandlers are assigned for each EditCommand,

CancelCommand and UpdateCommand (DataBind THEN AddHandlers)

Inside each Job Activity, repeater item, basic data is displayed and
dgInstallers is populated with Installers

working on this Job Activity.

The user is permitted to add new installers by selecting ddlInstallers
and
clicking btnAddInstaller. The page posts

and the newly added installer appears in dgInstallers.
btnAddInstaller_Click is fired and code adds to the

collection of installers.

Everything seems to work just fine up to this point.

Now, when you click the EditCommandColumn 'Edit' Link it calls
dgInstallers_EditCommand, we set the edit index and

then DataBind. All well and good. When we click the EditCommandColumn
"Cancel", the page just posts. No event is

fired. If we instead click the EditCommandColumn "Update" link,
dgInstallers_EditCommand is fired once again.

When we hover the mouse over the Edit link, the actual link is:

javascript:__doPostBack('rptrJobActivities$_ctl0$dgJobActivityInstallers$_ctl2$_ctl0','')

When we hover the mouse over the Cancel link, the actual link is:

javascript:__doPostBack('rptrJobActivities$_ctl0$dgJobActivityInstallers$_ctl2$_ctl1','')

When we hover the mouse over the Update link, the actual link is:
javascript:__doPostBack('rptrJobActivities$_ctl0$dgJobActivityInstallers$_ctl2$_ctl0','')

Notice how the links for both Edit and Update are identical?

Please help me figure why the update calls the edit and the cancel
just
posts...

Thanks in advance.

-Rick

PS.

Here is all the code, there are objects behind the scenes but these
are
tried and true.

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="JobTracking.aspx.vb"

Inherits="Patio.PCMS.WebClient.JobTracking"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>JobTracking</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<LINK href="/WebClient/Styles.css" type="text/css" rel="stylesheet">
<script language="javascript" src="/WebClient/global.js"></script>
</HEAD>
<body onload="onDocumentReady();">
<form id="Form1" method="post" runat="server">
<asp:datagrid id=dgContracts runat="server"
AutoGenerateColumns="False"
DataSource="<%#

Contracts %>">
<Columns>
<asp:EditCommandColumn CancelText="Cancel" EditText="Edit"

UpdateText="Upate"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="Job #">
<ItemTemplate>
<asp:Label runat="server" Text='<%#

DataBinder.Eval(Container, "DataItem.Job.JobNumber") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Product">
<ItemTemplate>
<asp:Label runat="server" Text='<%#

DataBinder.Eval(Container, "DataItem.Product.Description") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text='<%#

DataBinder.Eval(Container, "DataItem.Job.Status") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"

ItemStyle-CssClass="submit" CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<asp:repeater id=rptrJobActivities runat="server" DataSource="<%#

JobActivities(SelectedContractId)%>">
<ItemTemplate>
<tr style="FONT-WEIGHT: bold; COLOR: white; BACKGROUND-COLOR:
navy">
<td>
Description
</td>
<td>
Planned Start
</td>
<td>
Planned End
</td>
<td>
Actual End
</td>
<td>
Amount Due
</td>
<td>
Status
</td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container,

"DataItem.Description") %>
</td>
<td>
<%# DataBinder.Eval(Container,

"DataItem.PlanStartDate.Date").ToShortDateString %>
</td>
<td>
<%# DataBinder.Eval(Container,

"DataItem.PlanEndDate.Date").ToShortDateString %>
</td>
<td>
<%# DataBinder.Eval(Container,

"DataItem.ActEndDate.Date").ToShortDateString %>
</td>
<td>
<%# FormatCurrency(DataBinder.Eval(Container,

"DataItem.AmountDue"), 0) %>
</td>
<td>
<%# DataBinder.Eval(Container, "DataItem.Status") %>
</td>
</tr>
<tr>
<td colspan="3" align="center" valign="top">
Installers
<asp:DropDownList ID="ddlInstallerWorkers"

DataSource='<%# InstallerWorkers()%>' DataTextField="FullName"
DataValueField="WorkerId" Runat="server" />
<asp:Button CssClass="submit" ID="btnAddInstaller"

Text="Add" OnClick="btnAddInstaller_Click" Runat="server" />
<asp:DataGrid ID="dgJobActivityInstallers"

AutoGenerateColumns="False" Width="75%" Runat="server">
<HeaderStyle BackColor="LightGrey" />
<Columns>
<asp:EditCommandColumn

ButtonType="LinkButton" CancelText="Cancel" EditText="Edit"
UpdateText="Update"/>
<asp:TemplateColumn>
<HeaderTemplate>
Installer
</HeaderTemplate>
<ItemTemplate>
<%#

DataBinder.Eval(Container, "DataItem.FullName") %>
</ItemTemplate>
<EditItemTemplate>
<%#

DataBinder.Eval(Container, "DataItem.FullName") %>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Lead Installer?
</HeaderTemplate>
<ItemTemplate>
<%#

DataBinder.Eval(Container, "DataItem.LeadFlag") %>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox

ID="chkLeadInstaller" Checked='<%# DataBinder.Eval(Container,
"DataItem.LeadFlag") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
<td colspan="3" align="center" valign="top">
Comments
<asp:Button CssClass="submit"

ID="btnAddJobActivityComment" Text="Add" Runat="server" />
<asp:DataGrid AutoGenerateColumns="False"

ID="dgJobActivityComments" width="75%" Runat="server">
<HeaderStyle BackColor="LightGrey" />
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
Date
</HeaderTemplate>
<ItemTemplate>
<%#

DataBinder.Eval(Container, "DataItem.CommDate") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Comments
</HeaderTemplate>
<ItemTemplate>
<%#

DataBinder.Eval(Container, "DataItem.Comments") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox

ID="txtComments" Rows="4" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</ItemTemplate>
</asp:repeater>
</table>
</form>
</body>
</HTML>

Imports Patio.PCMS.Library
Imports Patio.EAF

Public Class JobTracking
Inherits System.Web.UI.Page

#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 dgContracts As
System.Web.UI.WebControls.DataGrid
Protected WithEvents rptrJobActivities As
System.Web.UI.WebControls.Repeater

'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

Protected ReadOnly Property CustomerId() As Guid
Get
Return New Guid(Request.QueryString("CustomerID"))
End Get
End Property

Protected ReadOnly Property LeadId() As Guid
Get
Return New Guid(Request.QueryString("LeadID"))
End Get
End Property

Protected ReadOnly Property Customer() As Customer
Get
Return CType(Session(CustomerId.ToString), Customer)
End Get
End Property

Protected ReadOnly Property Lead() As Lead
Get
Return CType(Customer.Leads(LeadId), Lead)
End Get
End Property

Protected ReadOnly Property Contracts() As ContractCollection
Get
Return Lead.Contracts
End Get
End Property

Protected Property SelectedContractId() As Guid
Get
If Not Session("SelectedContractId") Is Nothing Then
Return New Guid(CType(Session("SelectedContractId"), String))
End If
End Get
Set(ByVal Value As Guid)
Session("SelectedContractId") = Value
End Set
End Property

Protected ReadOnly Property JobActivities() As JobActivityCollection
Get
Try
Return Contracts(SelectedContractId).Job.JobActivities
Catch ex As Exception
'
End Try

End Get
End Property

Protected ReadOnly Property JobActivities(ByVal ContractId As Guid)
As
JobActivityCollection
Get
Try
Return Contracts(ContractId).Job.JobActivities
Catch ex As Exception
'
End Try
End Get
End Property

Protected Property SelectedJobActivityId() As Guid
Get
If Not CType(Session("SelectedJobActivityId"), String) Is Nothing
Then
Return New Guid(CType(Session("SelectedJobActivityId"), String))
End If
End Get
Set(ByVal Value As Guid)
Session("SelectedJobActivityId") = Value.ToString
End Set
End Property

Protected ReadOnly Property JobActivityInstallers() As
JobActivityInstallerCollection
Get
Return JobActivityInstallers(SelectedJobActivityId)
End Get
End Property

Protected ReadOnly Property JobActivityInstallers(ByVal JobActivityId
As
Guid) As JobActivityInstallerCollection
Get
Return JobActivities()(JobActivityId).Installers
End Get
End Property

Protected ReadOnly Property InstallerWorkers() As
ReadOnlyWorkerCollection
Get
Return
ReadOnlyWorkerCollection.GetReadOnlyWorkerCollection(Customer.BusinessUnit.BusinessUnit,
True)
End Get
End Property

Protected ReadOnly Property Comments() As CommentCollection
Get
Return
CommentCollection.GetCommentCollection(ReadOnlyCommentType.GetReadOnlyCommentType("X"),
Me.SelectedJobActivityId, CommentCollection.UserFlagStateEnum.All)
End Get
End Property

'Private Sub BindJobActivityInstallersGrid(ByVal dgInstallers As
DataGrid,
ByVal JobActivityId As Guid)
' dgInstallers.DataSource = JobActivityInstallers(JobActivityId)
' dgInstallers.DataBind()
'End Sub

'Private Sub BindJobActivityInstallersGrid(ByVal dgInstallers As
DataGrid)
' dgInstallers.DataSource = JobActivityInstallers()
' dgInstallers.DataBind()
'End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If CustomerId.Equals(Guid.Empty) OrElse LeadId.Equals(Guid.Empty)
Then
Response.Redirect("LeadSearch.aspx", True)
End If

If Not IsPostBack Then
dgContracts.DataBind()
End If
End Sub

Private Sub dgContracts_ItemCommand(ByVal source As Object, ByVal e
As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgContracts.ItemCommand
If e.CommandName = "Select" Then
Dim itm As Contract = Contracts(e.Item.ItemIndex)

If Not itm Is Nothing Then
Session("SelectedContractId") = itm.ContractId.ToString

JobActivities.SortBySequence(SortConstants.SortAscending)

rptrJobActivities.DataSource = JobActivities
rptrJobActivities.DataBind()
End If
End If
End Sub

Private Sub rptrJobActivities_ItemCreated(ByVal sender As Object,
ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
rptrJobActivities.ItemCreated
Dim itm As JobActivity = JobActivities()(e.Item.ItemIndex)

If Not itm Is Nothing Then
Dim dgInstallers As DataGrid =
CType(e.Item.FindControl("dgJobActivityInstallers"), DataGrid)
Dim dbComments As DataGrid =
CType(e.Item.FindControl("dgJobActivityComments"), DataGrid)

dgInstallers.DataSource = JobActivityInstallers(itm.JobActivityId)
dgInstallers.DataBind()

AddHandler dgInstallers.EditCommand, AddressOf
dgJobActivityInstallers_EditCommand
AddHandler dgInstallers.UpdateCommand, AddressOf
dgJobActivityInstallers_UpdateCommand
AddHandler dgInstallers.CancelCommand, AddressOf
dgJobActivityInstallers_CancelCommand

dbComments.DataSource = Me.Comments
dbComments.DataBind()
End If
End Sub

Protected Sub btnAddInstaller_Click(ByVal sender As Object, ByVal e
As
EventArgs)
Dim btn As Button = CType(sender, Button)
Dim ri As RepeaterItem = CType(btn.Parent, RepeaterItem)
Dim ddl As DropDownList =
CType(ri.FindControl("ddlInstallerWorkers"),
DropDownList)
Dim dgInstallers As DataGrid =
CType(ri.FindControl("dgJobActivityInstallers"), DataGrid)

SelectedJobActivityId =
JobActivities(SelectedContractId)(ri.ItemIndex).JobActivityId

Dim itm As ReadOnlyWorker = InstallerWorkers(ddl.SelectedIndex)

If Not itm Is Nothing Then
If Not JobActivityInstallers Is Nothing Then
If Not JobActivityInstallers.Contains(itm.WorkerId) Then
JobActivityInstallers.Add(itm.WorkerId)
'dgInstallers.EditItemIndex = -1

dgInstallers.DataSource = JobActivityInstallers()
dgInstallers.DataBind()

End If
End If
End If
End Sub

Protected Sub dgJobActivityInstallers_EditCommand(ByVal source As
Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
WriteDebugMessage(HttpContext.Current, Me,
"dgJobActivityInstallers.EditCommand")
Dim dgInstallers As DataGrid = CType(source, DataGrid)

dgInstallers.EditItemIndex = e.Item.ItemIndex

'dgInstallers.DataSource = JobActivityInstallers()
dgInstallers.DataBind()

WriteDebugMessage(HttpContext.Current, Me,
"dgJobActivityInstallers.EditCommand " & dgInstallers.EditItemIndex)
End Sub

Protected Sub dgJobActivityInstallers_CancelCommand(ByVal source As
Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
WriteDebugMessage(HttpContext.Current, Me,
"dgJobActivityInstallers.CancelCommand")
Dim dgInstallers As DataGrid = CType(source, DataGrid)

dgInstallers.EditItemIndex = -1

'dgInstallers.DataSource = JobActivityInstallers()
dgInstallers.DataBind()
End Sub

Protected Sub dgJobActivityInstallers_UpdateCommand(ByVal source As
Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
WriteDebugMessage(HttpContext.Current, Me,
"dgJobActivityInstallers.UpdateCommand")
Dim dgInstallers As DataGrid = CType(source, DataGrid)

dgInstallers.EditItemIndex = -1

'dgInstallers.DataSource = JobActivityInstallers()
dgInstallers.DataBind()
End Sub

End Class
 
R

Richard Ryerson

Thanks for all the wonderful comments helping me get through this problem.

It is amazing to see so many helpful people gather in one place.

-Rick
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top