ProtectedSub fvApplicantComments_ItemCommand(ByVal
sender AsObject,
ByVal e
As System.Web.UI.WebControls.FormViewCommandEventArgs)
Handles fvApplicantComments.ItemCommand
If e.CommandName =
"Edit"Then
Dim row
As FormViewRow = fvApplicantComments.Row
Dim employeeIDLabel
As Label =
CType(row.FindControl("employee_idLabel"),
Label)
If employeeIDLabel
IsNotNothingThen
If employeeIDLabel.Text <> Session("user_employee_id")
Then fvApplicantComments.ChangeMode(FormViewMode.ReadOnly)
ProtectedSub fvApplicantComments_ModeChanging(ByVal
sender AsObject,
ByVal e
As System.Web.UI.WebControls.FormViewModeEventArgs)
Handles fvApplicantComments.ModeChanging
SelectCase e.NewMode
Case FormViewMode.Edit
Dim employeeIDLabel
As Label = fvApplicantComments.FindControl("employee_idLabel")
IfNot employeeIDLabel
IsNothingThen
If employeeIDLabel.Text <> Session("user_employee_id")
Then e.Cancel =
True
Protected Sub fvApplicantComments_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvApplicantComments.ModeChanged
If fvApplicantComments.DefaultMode = FormViewMode.Edit Then
Dim employeeIDLabel As Label = CType(fvApplicantComments.FindControl("employee_idLabel"), Label)
If employeeIDLabel IsNot Nothing Then
If employeeIDLabel.Text <> Session("user_employee_id") Then
fvApplicantComments.ChangeMode(FormViewMode.ReadOnly)
End If
End If
End If
End Sub
skujan57
0 Points
7 Posts
can't find FormView control in ItemCommand event
Dec 22, 2012 03:22 PM|LINK
When I run the below code the employeeIDLabel has a value of NOTHING. Shouldn't the FindControl method work? I referenced the following: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemcommand.aspx
'aspx.vb
Protected Sub fvApplicantComments_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fvApplicantComments.ItemCommand
If e.CommandName = "Edit" Then
Dim row As FormViewRow = fvApplicantComments.Row
Dim employeeIDLabel As Label = CType(row.FindControl("employee_idLabel"), Label)
If employeeIDLabel IsNot Nothing Then
If employeeIDLabel.Text <> Session("user_employee_id") Then fvApplicantComments.ChangeMode(FormViewMode.ReadOnly)
End If
End If
End Sub
'aspx
<EditItemTemplate>
<asp:Label ID="employee_idLabel" runat="server" Text='<%# Bind("employee_id") %>' Visible="true" />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</EdittemTemplate>
<ItemTemplate>
<asp:Label ID="employee_idLabel" runat="server" Text='<%# Bind("employee_id") %>' Visible="true" />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
paindaasp
Star
12050 Points
2034 Posts
Re: can't find FormView control in ItemCommand event
Dec 22, 2012 03:31 PM|LINK
Try going directly to the FormView:
Dim employeeIDLabel As Label = DirectCast(fvapplicantComments.FindControl("employeeIDLabel"), Label)skujan57
0 Points
7 Posts
Re: can't find FormView control in ItemCommand event
Dec 22, 2012 03:47 PM|LINK
Sorry, that didn't work.
skujan57
0 Points
7 Posts
Re: can't find FormView control in ItemCommand event
Dec 22, 2012 06:08 PM|LINK
I got it to work by using the ModeChanged event:
Protected Sub fvApplicantComments_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles fvApplicantComments.ModeChanging
Select Case e.NewMode
Case FormViewMode.Edit
Dim employeeIDLabel As Label = fvApplicantComments.FindControl("employee_idLabel")
If Not employeeIDLabel Is Nothing Then
If employeeIDLabel.Text <> Session("user_employee_id") Then e.Cancel = True
End If
End Select
End Sub
oned_gk
All-Star
31023 Points
6355 Posts
Re: can't find FormView control in ItemCommand event
Dec 23, 2012 01:35 AM|LINK
Protected Sub fvApplicantComments_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvApplicantComments.ModeChanged If fvApplicantComments.DefaultMode = FormViewMode.Edit Then Dim employeeIDLabel As Label = CType(fvApplicantComments.FindControl("employee_idLabel"), Label) If employeeIDLabel IsNot Nothing Then If employeeIDLabel.Text <> Session("user_employee_id") Then fvApplicantComments.ChangeMode(FormViewMode.ReadOnly) End If End If End If End Sub