locked
Hidden field missing value on Page_load function RRS feed

  • Question

  • User1738843376 posted

    Hi,

    I have the following code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
        If Not Page.IsPostBack() Then
            hid_action.value = request.params("action") & String.Empty
            hid_id.value = request.params("id") & String.Empty
        End If
    
        Response.Write(DateTime.UtcNow.ToString("hh:mm:ss fffffff") & " hid_action.Value in Page_Load:  " & hid_action.Value & "<br />")
    
        If hid_action.value <> "new" Then        
            btn_save.Visible = False
            btn_update.Visible = True
            btn_delete.Visible = True
        Else
            btn_save.Visible = True
            btn_update.Visible = False
            btn_delete.Visible = False
        End If
    
    End Sub
    
    Protected Sub mySelect_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles mySelect.SelectedIndexChanged
    
        Response.Write(DateTime.UtcNow.ToString("hh:mm:ss fffffff") & " hid_action.Value in mySelect_SelectedIndexChanged:  " & hid_action.Value & "<br />")
    
        If hid_action.value <> "new"
            SomeFunctionToGetData(hid_id.value, mySelect.SelectedValue)
        End If
    
    End Sub
    
      
    
    

    On my initial call to the page (not postback) i successfully capture the 'action' value from the URL and assign it to the hidden field and the buttons appear as they should, responding to the fact that the 'action' value is 'new', so it shows the 'btn_save' and hides both others and also writes the 'hid_action' value to the screen with the expected value.

    Now when i use the selector and change its selected value, the first response to the screen is the one from the 'page_load' event, and it comes with an empty value for the 'hid_action' which it should not, since this is a postback and it should retain the value that was previously on the hidden field. As a result of this, the buttons visibility invert, and now the 'btn_save' is hidden while the 'btn_update' and 'btn_delete' are both visible, which they should not.

    Then the second response appears, from within the 'mySelect_SelectedIndexChanged' event and it correctly reflects what the hidden field value should be, contrary to what just happened when the 'page_load' event that ran milliseconds before.

    I would expect this behaviour if i was using the 'page_init' event, but on the 'page_load'.

    Not sure if it makes a difference, but all this code and the ASP elements are inside a usercontrol (ASCx file), not directly on an ASPx file.

    Can somebody explain me what the heck is going on and provide some direction towards a solution?

    Thanks in advance

    Friday, May 18, 2018 6:12 PM

All replies

  • User283571144 posted

    Hi 0belix,

    According to your description and codes, I have created a test demo on my side, it works well.

    The hidden file vale doesn't loss when the drop down list selected index changed.

    Have you closed the viewstate?

    Could you please post the details codes about your usecontrol and codes about page which used user control?

    My test demo codes:

    Call user control:

        <form id="form1" runat="server">
            <div>
                <uc1:WebUserControl1 runat="server" id="WebUserControl1" />
            </div>
        </form>

    Usercontrol:

    <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="VBidentityTrue.WebUserControl1" %>
    <asp:HiddenField ID="hid_action" runat="server" />
    <asp:HiddenField ID="hid_id" runat="server" />
    
    
    
    <br>
    <asp:Button ID="btn_save" runat="server" Text="btn_save" />
    <asp:Button ID="btn_update" runat="server" Text="btn_update" />
    <asp:Button ID="btn_delete" runat="server" Text="btn_delete" />
    
    <br/>
     
    <asp:DropDownList ID="mySelect" runat="server"  OnSelectedIndexChanged="mySelect_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem>
            aaaa
        </asp:ListItem>
            <asp:ListItem>
            bbbbb
        </asp:ListItem>
    </asp:DropDownList>
    

    Code-behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack() Then
                hid_action.Value = Request.Params("action") & String.Empty
                hid_id.Value = Request.Params("id") & String.Empty
            End If
    
            Response.Write(DateTime.UtcNow.ToString("hh:mm:ss fffffff") & " hid_action.Value in Page_Load:  " & hid_action.Value & "<br />")
    
            If hid_action.Value <> "new" Then
                btn_save.Visible = False
                btn_update.Visible = True
                btn_delete.Visible = True
            Else
                btn_save.Visible = True
                btn_update.Visible = False
                btn_delete.Visible = False
            End If
        End Sub
        Protected Sub mySelect_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles mySelect.SelectedIndexChanged
    
            Response.Write(DateTime.UtcNow.ToString("hh:mm:ss fffffff") & " hid_action.Value in mySelect_SelectedIndexChanged:  " & hid_action.Value & "<br />")
    
            If hid_action.Value <> "new" Then
    
            End If
    
        End Sub

    Result

    :

    Best Regards,

    Brando

    Monday, May 21, 2018 5:22 AM
  • User1738843376 posted

    Hi Brando,

    Thx for the reply.

    I can't place here the full code as there are hundreds of code lines on it.

    The code i posted is a simplification as it illustrates where the issue is happening. The only thing i just mentioned in a rush and did not provide details was the following:

    The project was initially developed before the launch of masterpages, so we simulate them by using a default page where the menu, and all other elements that repeat from page to page get centralized, and then we capture from the URL the control to load by retrieving the querystring "ctrl". After getting the requested control, we load it on the default.aspx page_load using the following code:

    Dim recievedCtrl As String = Request.Params("ctrl")
    
    If recievedCtrl <> "" Then
         If recievedCtrl = "default" Then
              Response.Redirect("default.aspx")
         End If
         Me.toLoadControlContent = LoadControl(recievedCtrl & ".ascx")
         Me.mainContent.Controls.Clear()
         Me.mainContent.Controls.Add(Me.toLoadControlContent)
    Else
         Me.toLoadControlContent = LoadControl("home.ascx")
         Me.mainContent.Controls.Clear()
         Me.mainContent.Controls.Add(Me.toLoadControlContent)
    End If

    This is where the code i posted earlier comes into action, since it is placed inside a usercontrol, as explained, that is called using the above method.

    My only guess is that the page cycle gets messed up when loading the control using this approach, but i'm not sure if that is the case, or how to overcome this without rebuilding the whole page and possibly the project, and that is not an option at this point.

    I can also add that we are using the 4.0 framework under IIS 10.0.16299.15

    Does this help you helping me in any way?

    Best,

    Monday, May 21, 2018 10:49 AM
  • User753101303 posted

    Hi,

    According to https://msdn.microsoft.com/en-us/library/ms178472.aspx it seems you could use LoadComplete instead to have the guarantee that Load was previously called on all controls...

    Monday, May 21, 2018 1:35 PM