Page.Load event fired before RowCommand or OnClick command cause me a problem with a Formview Default Mode

Posted on 2006-06-03
Last Modified: 2008-01-09
Hi experts,

I have a gridview control that is displaying a WebUserControl that is containing a FormView control to display or edit the details of the selected item.
I have also a button to insert a new record in the database.

I have created 2 public property to help me control the way to display the formview and when the data has been updated to refresh the GridView.

    Public Event DataUpdated As EventHandler
    Public DefaultDisplayMode As String

    Protected Overridable Sub OnDataUpdated(ByVal e As EventArgs)
        RaiseEvent DataUpdated(Me, e)
    End Sub

The problem I have is with the DefaultDisplayMode.
When I click the button, I want to display the formview in Insert mode. So in the Page.Load of the WebUserControl, Iput the following code:

WUC Page.Load Sub:

        If DefaultDisplayMode = "Insert" Then
            fvTelephoneInfos.DefaultMode = FormViewMode.Insert
            CType(fvTelephoneInfos.FindControl("EquipmentTypeLabel"), Label).Text = "Telephone"
        End If

OnClick Sub:

    Protected Sub btnAddConfiguration_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddConfiguration.Click
        '        Dim strCommand As String
        Dim strCustID As String

        strCustID = Session("CustID")

        Select Case ddlEquipmentType.SelectedValue
            Case "Telephone"
                WucTelephoneDetails1.DefaultDisplayMode = "Insert"
                WucTelephoneDetails1.Visible = True


The problem, is that the WebUserControl Page.Load event is lunched before the Button OnClick event or Gridview RowCommand, so the DefaultDisplayMode is not yet initialized.

Is there a way to tels the WebUserControl that the DefaultDisplayMode property has changed, or processed the WebUserControl Page.Load event after the Onclick and RowCommand events.


Question by:SergeGregoire
    LVL 14

    Expert Comment

    It is not a problem. It is the way how ASP.NET works. Page_Load always get fired, also on postbacks. To prevent code from being executed which is in the Page_Load use this code:

    If Not IsPostBack Then
        // code gets executed only when page gets loaded for the first time
    End If
    LVL 14

    Expert Comment

    Hope I made the VB.NET code correct :P
    LVL 1

    Author Comment

    I found a solution myself.

    Since the Page.Load event is fired before the Gridview.RowCommand and Button.OnClick, I put my test condition in the Page_Prerender event.
    Since the Prerender event is fire just before to display the page, the DefaultDisplayMode is set and the formview appear in the appropriate mode.

       Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
            If DefaultDisplayMode = "Insert" Then
                fvTelephoneInfos.DefaultMode = FormViewMode.Insert
            End If
        End Sub

    Thanks anyway for your help existenz2, but your comment did not help me to fix my problem.
    LVL 1

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now