Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > strange inconsistency referencing controls in Formview itemtemplate

Reply
Thread Tools

strange inconsistency referencing controls in Formview itemtemplate

 
 
Ned Balzer
Guest
Posts: n/a
 
      08-03-2006
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

 
Reply With Quote
 
 
 
 
Ned Balzer
Guest
Posts: n/a
 
      08-03-2006
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

Ned Balzer wrote:
> 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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=
Guest
Posts: n/a
 
      08-03-2006
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.

"Ned Balzer" wrote:

> 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
>
>

 
Reply With Quote
 
Ned Balzer
Guest
Posts: n/a
 
      08-03-2006
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

William Sullivan wrote:
> 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.
>
> "Ned Balzer" wrote:
>
> > 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
> >
> >


 
Reply With Quote
 
Ned Balzer
Guest
Posts: n/a
 
      08-03-2006
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


Ned Balzer wrote:
> 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
>
> William Sullivan wrote:
> > 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.
> >
> > "Ned Balzer" wrote:
> >
> > > 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
> > >
> > >


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing Controls on ItemTemplate in FormView Control mike@5starserv.com ASP .Net Web Controls 2 01-28-2007 01:23 PM
Referencing controls within a Formview msch-prv@bluewin.ch ASP .Net 1 07-26-2006 05:21 PM
Referencing child controls on a formview Neil ASP .Net 1 02-08-2006 09:32 AM
Referencing child controls on a formview Neil ASP .Net 0 02-07-2006 10:34 AM
can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate? Bennett Haselton ASP .Net 1 09-24-2004 01:59 AM



Advertisments