DetailsView Mode change event

Last post 11-07-2009 6:10 PM by MetalAsp.Net. 7 replies.

Sort Posts:

  • DetailsView Mode change event

    11-06-2009, 3:17 PM
    • Member
      157 point Member
    • mjta
    • Member since 09-18-2008, 7:10 PM
    • Posts 367

    My DetailsView has the AutoGenerateInsert set to True for Default.  When the page loads and there are no rows, the Insert commands are not there.  WHen I delete the condition - DetailsView1.AutoGenerateInsertButton=False" - and the page loads, the Insert commands are there.  According to my statement, if there are no rows, the insert commands should be generated.  Why is this happening?

     Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
            If DetailsView1.Rows.Count = 0 Then
                DetailsView1.ChangeMode(DetailsViewMode.Insert)
                DetailsView1.AutoGenerateInsertButton = True
            Else
                DetailsView1.AutoGenerateInsertButton = False
    
            End If
        End Sub


     

  • Re: DetailsView Mode change event

    11-06-2009, 4:08 PM
    • Contributor
      3,384 point Contributor
    • Danish Ali
    • Member since 08-08-2008, 7:22 PM
    • Fort Lauderdale, US
    • Posts 468

    Write your code in DetailsView PreRender event instead of DataBound event as follows and it will work.


     Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
            If DetailsView1.Rows.Count = 0 Then
                'DetailsView1.DefaultMode = DetailsViewMode.Insert
                DetailsView1.ChangeMode(DetailsViewMode.Insert)
                DetailsView1.AutoGenerateInsertButton = True
            Else
                'DetailsView1.DefaultMode = DetailsViewMode.ReadOnly
                DetailsView1.AutoGenerateInsertButton = False
            End If
        End Sub

     Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender

            If DetailsView1.Rows.Count = 0 Then

                'DetailsView1.DefaultMode = DetailsViewMode.Insert

                DetailsView1.ChangeMode(DetailsViewMode.Insert)

                DetailsView1.AutoGenerateInsertButton = True

            Else

                'DetailsView1.DefaultMode = DetailsViewMode.ReadOnly

                DetailsView1.AutoGenerateInsertButton = False


            End If

        End Sub


    Hope it will help.


    If my post solves your problem, please mark it as an answer.
  • Re: DetailsView Mode change event

    11-06-2009, 4:19 PM

    In my quick test, it worked with this:

     If DetailsView1.Rows.Count = 0 Then   
               DetailsView1.ChangeMode(DetailsViewMode.Insert)   
               DetailsView1.DataBind()   
    End If 


     

    Not a VB person, so I may have screwed up the call.  You get the idea though; you need to call databind.

  • Re: DetailsView Mode change event

    11-07-2009, 4:33 PM
    • Member
      157 point Member
    • mjta
    • Member since 09-18-2008, 7:10 PM
    • Posts 367

    Sorry, MetalAsp.Net.....your solution did not help me. 

  • Re: DetailsView Mode change event

    11-07-2009, 4:45 PM
    • Contributor
      3,384 point Contributor
    • Danish Ali
    • Member since 08-08-2008, 7:22 PM
    • Fort Lauderdale, US
    • Posts 468

    Have you tried my solution? Just curious is it working for you.

    If it is working, please mark it as answer, so that it can help others too.


    Thanks.

    If my post solves your problem, please mark it as an answer.
  • Re: DetailsView Mode change event

    11-07-2009, 5:49 PM
    Answer
    • Member
      157 point Member
    • mjta
    • Member since 09-18-2008, 7:10 PM
    • Posts 367

    I did try it, and it seemed to work better than what I had, but there were still issues. 

    I ended up changing the default mode to False for the AutoGenerateInsert and it helped even more.

    This is where I am now, it seems to be working ok. 

    Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
            If DetailsView1.Rows.Count = 0 Then
                DetailsView1.AutoGenerateInsertButton = True
                DetailsView1.ChangeMode(DetailsViewMode.Insert)
            End If
    
        End Sub
    
        Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
            DetailsView1.AutoGenerateInsertButton = False
        End Sub

    Now, I am faced with another issue and I'm in the middle of posting it.

    thanks for your attention.
     

  • Re: DetailsView Mode change event

    11-07-2009, 6:05 PM
    • Member
      157 point Member
    • mjta
    • Member since 09-18-2008, 7:10 PM
    • Posts 367

    cancel my intention to post that subsequent problem...still working on it.... 

  • Re: DetailsView Mode change event

    11-07-2009, 6:10 PM

    mjta:

    Sorry, MetalAsp.Net.....your solution did not help me. 

     

    No problem.  Sorry it didn't work for you.  In my test it worked for me.  But, of course, your HTML might look very different from mine.  Good luck.

Page 1 of 1 (8 items)