asp.net标题标签不会在页面加载时填充变量

时间:2013-05-10 10:30:54

标签: asp.net variables labels pageload prerender

我已声明变量并从我的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>

1 个答案:

答案 0 :(得分:1)

我没有检查过,但我认为问题是Page_load时间,实际上没有selectditem。我建议尝试绑定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页面中删除标签上的所有prerender事件。

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

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

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