Question

I have declared variables and populated from dropdownlists in my asp. However on pageload the labels aren't populating, they only populate once I change values in the dropdowns. So the variables are working with the labels, just not initially on page load.

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim strDataCollection As String = ddlDataCollection.Text
    Dim strYear As String = DdlYear.Text
    Dim strSubject As String = DdlSubject.Text
    Dim strTeachingGroup As String = DdlTeachingGroup.Text
    Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text

    lblHeaderYear.Text = "Year " & strYear
    lblHeaderDataCollection.Text = " " & strDataCollection
    lblHeaderSubject.Text = " " & strSubject
    lblHeaderTeachingGroup.Text = " " & strTeachingGroup
    lblHeaderSubroup.Text = " " & strSubgroup

    If strTeachingGroup = "Select All" Then
        lblHeaderTeachingGroup.Visible = False
    Else
        lblHeaderTeachingGroup.Visible = True
    End If

    If strSubgroup = "Select All" Then
        lblHeaderSubroup.Visible = False
    Else
        lblHeaderSubroup.Visible = True
    End If

End Sub

The asp for my labels is:

<div class="centeronpage">
          <asp:Label ID="lblHeaderYear" runat="server" Text="" CssClass="header"></asp:Label>
          <asp:Label ID="lblHeaderDataCollection" runat="server" Text="" CssClass="header"></asp:Label>
          <asp:Label ID="lblHeaderSubject" runat="server" Text="" CssClass="header"></asp:Label>
          <asp:Label ID="lblHeaderTeachingGroup" runat="server" Text="" CssClass="header"></asp:Label>
          <asp:Label ID="lblHeaderSubroup" runat="server" Text="" CssClass="header"></asp:Label>
</div>

EDIT ANSWER

I created the following PreRender as suggested below. I then included OnPreRender="Page_PreRender" in one of the label elements and this seemed to trigger the prerender.

Protected Sub Page_PreRender(sender As Object, e As EventArgs)

    Dim strDataCollection As String = ddlDataCollection.Text
    Dim strYear As String = DdlYear.Text
    Dim strSubject As String = DdlSubject.Text
    Dim strTeachingGroup As String = DdlTeachingGroup.Text
    Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text

    lblHeaderYear.Text = "Year " & strYear
    lblHeaderDataCollection.Text = " " & strDataCollection
    lblHeaderSubject.Text = " " & strSubject
    lblHeaderTeachingGroup.Text = " " & strTeachingGroup
    lblHeaderSubroup.Text = " " & strSubgroup

End Sub

EDIT - DDL Binding

<asp:Label ID="lblHeaderYear" runat="server" Text="" CssClass="header" OnPreRender="Page_PreRender"></asp:Label>
<asp:Label ID="lblHeaderDataCollection" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubject" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderTeachingGroup" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubroup" runat="server" Text="" CssClass="header"></asp:Label>
Was it helpful?

Solution

I've not checked it, but I think the problem is as the Page_load time, there's actually no selectditem. I would suggest trying to bind the labels in the prerender event, where dropdowns should actually have selected items (the first one if you didn't specify anything else)

EDIT :

you should do that for your prerender event :

Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.Load

    Dim strDataCollection As String = ddlDataCollection.Text
    Dim strYear As String = DdlYear.Text
    Dim strSubject As String = DdlSubject.Text
    Dim strTeachingGroup As String = DdlTeachingGroup.Text
    Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text

    lblHeaderYear.Text = "Year " & strYear
    lblHeaderDataCollection.Text = " " & strDataCollection
    lblHeaderSubject.Text = " " & strSubject
    lblHeaderTeachingGroup.Text = " " & strTeachingGroup
    lblHeaderSubroup.Text = " " & strSubgroup

End Sub

Note the "Handles Me.Load" after your PreRender event declaration. This is how you actually override the page's PreRender event , exactly as you do it for your PageLoad. You should then remove all prerender event on your labels in you aspx page.

Please note that you can also make use of the AutoEventWireup Page attribute :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

This will allow you, for Page events only, to tell .NET to automatically override page events if a method with the same name is defined in your codebehind. In other words, you'd be able to remove the "Handles Me.xxx" after event declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top