strange inconsistency referencing controls in Formview itemtemplate

N

Ned Balzer

Can anyone explain why in the following code, where btnCancel1 is a
Button inside the itemTemplate of the formview, the first call to
change the text of the button, in the Page_Load sub, works fine, but
the second call, in the bookFormView_ItemUpdated sub, throws an
exception (Object reference not set to an instance of an object)? And
if you know why, can you suggest a workaround?

Here is the code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not Page.IsPostBack Then
bookFormView.ChangeMode(FormViewMode.ReadOnly)
Dim cancelButton As Button =
CType(bookFormView.FindControl("btnCancel1"), Button)
cancelButton.Text = "Cancel"
End If
End Sub

Protected Sub bookFormView_ItemUpdated(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles
bookFormView.ItemUpdated
Dim cancelButton1 As Button =
CType(bookFormView.FindControl("btnCancel1"), Button)
cancelButton1.Text = "Done"
End Sub



The line of code where the error occurs is cancelButton1.Text = "Done"

Thanks in advance.

-- Ned
 
N

Ned Balzer

Followup: In researching this I came across a comment that only the
controls in the current mode are accessible; if the formview is in edit
mode then only the controls in the <edititemtemplate> are accessible,
if in read-only mode then only the controls in the <itemtemplate> are
accessible. This might explain this except that an update is supposed
to automatically return the formview to read-only mode, right? Or does
that happen later (after the formview_itemupdated() has terminated)?
So I tried explicitly changing the mode to read-only within the
itemupdated subroutine, but I still get the same error.

Hope someone can relieve my frustration.

-- Ned
 
G

Guest

1) Why not just use the control's variable? If the control has an id of
btnCancel1 and it's marked runat="server", then just do btnCancel1.Text =
"Done"

2) Are you using master pages? Control ID's change when placed into a
content placeholder on a master page. Use the DevToolBar to explore the DOM
and determine what your control's ID actually is on the client size.
 
N

Ned Balzer

1) When I try that, I get an error in Visual Web Developer 2005: "Name
'btnCancel1' is not declared." It is marked runat="server" but I think
the problem arises because it is buried within the formview, so it's
not a normal button control.

2) No, I am not currently using master pages.

-- Ned
 
N

Ned Balzer

Well, it doesn't make sense to me, but I found the suggestion somewhere
and tried it: calling bookformview.databind() before attempting to
reference the object, and the error went away. Problem is, the button
text is not being changed either.

Hmmmmmm
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top