0

我已经声明了变量并从我的 asp 中的下拉列表中填充。但是,在页面加载时,标签不会填充,它们只会在我更改下拉列表中的值时才会填充。因此变量与标签一起使用,而不是最初在页面加载时。

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

我的标签的 asp 是:

<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>

编辑答案

我按照以下建议创建了以下 PreRender。然后我包含 OnPreRender="Page_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

编辑 - DDL 绑定

<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>
4

1 回答 1

1

我没有检查它,但我认为问题出在 Page_load 时间,实际上没有选择项。我建议尝试在 prerender 事件中绑定标签,下拉菜单实际上应该有选择的项目(如果您没有指定其他任何内容,则为第一个)

编辑 :

你应该为你的预渲染事件这样做:

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

注意 PreRender 事件声明后的“Handles Me.Load”。这就是您实际覆盖页面的 PreRender 事件的方式,就像您为 PageLoad 所做的那样。然后,您应该在您的 aspx 页面中删除标签上的所有预渲染事件。

请注意,您还可以使用 AutoEventWireup Page 属性:

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

如果在代码隐藏中定义了具有相同名称的方法,这将允许您(仅针对 Page 事件)告诉 .NET 自动覆盖页面事件。换句话说,您可以在事件声明后删除“Handles Me.xxx”。

于 2013-05-10T10:33:52.523 回答