strange inconsistency referencing controls in Formview itemtemplate

Discussion in 'ASP .Net' started by Ned Balzer, Aug 3, 2006.

  1. Ned Balzer

    Ned Balzer Guest

    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
     
    Ned Balzer, Aug 3, 2006
    #1
    1. Advertising

  2. Ned Balzer

    Ned Balzer Guest

    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
     
    Ned Balzer, Aug 3, 2006
    #2
    1. Advertising

  3. RE: strange inconsistency referencing controls in Formview itemtemplat

    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
    >
    >
     
    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Aug 3, 2006
    #3
  4. Ned Balzer

    Ned Balzer Guest

    Re: strange inconsistency referencing controls in Formview itemtemplat

    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
    > >
    > >
     
    Ned Balzer, Aug 3, 2006
    #4
  5. Ned Balzer

    Ned Balzer Guest

    Re: strange inconsistency referencing controls in Formview itemtemplat

    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
    > > >
    > > >
     
    Ned Balzer, Aug 3, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,407
    Joe Fallon
    Sep 24, 2004
  2. Neil
    Replies:
    0
    Views:
    574
  3. Neil
    Replies:
    1
    Views:
    491
  4. Replies:
    1
    Views:
    464
  5. Replies:
    2
    Views:
    380
Loading...

Share This Page