ASP用户控制问题

时间:2010-04-08 19:55:23

标签: asp.net vb.net

我正在尝试使用来自多个来源的代码构建我自己的日期选择器。

为什么在可见时隐藏日历?

myDate.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="myDate.ascx.vb"
    Inherits="Website.myDate" %>

<asp:TextBox ID="dateText" runat="server" > </asp:TextBox>
<asp:Button ID="dateBtn" runat="server" UseSubmitBehavior="false" Text="x" />
<asp:Calendar ID="dateCal" runat="server" ></asp:Calendar>

myDate.ascx.vb

Partial Public Class myDate
    Inherits System.Web.UI.UserControl

    Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
        dateText.Text = dateCal.SelectedDate ' Update text box'
        dateCal.Visible = False              ' Hide calendar'
    End Sub

    Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
        dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
    End Sub

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dateCal.Visible = False ' Hide calendar on load'
    End Sub

    Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
        dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
    End Sub
End Class

2 个答案:

答案 0 :(得分:2)

在可见时它不会隐藏,因为每次加载页面时都会运行Page_Load,包括回发(按钮点击等)。

所以你需要使用 IsPostBack 属性来设置Page_load中的visiblity:

if Not Page.IsPostBack then
    dateCal.Visible = False ' Hide calendar on load'
end if

有时在使用IsPostBack的page_load中进行初始化可能会导致大量奇怪的情况,因此请明智地使用它。

我想我倾向于在ascx文件中而不是在Page_load中设置属性。

答案 1 :(得分:1)

首先,如果要在页面回发之间切换控件,则需要使用ViewState。检查,您的页面是否使用ViewState。

在您的情况下,页面的EnableViewState必须设置为true。

另外,请检查您的Page_load功能。

每次加载您隐藏日历的页面

每次按钮或日历上升之前,Page_load都会调用。 因此,您将可见性更改为true,然后在事件中使用已更改的可见性值:

dateCal.Visible = Not dateCal.Visible

点击dateBtn

时,日历始终可见

为了向您明确,我将按照他们呼叫的顺序在您的代码中订购事件:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dateCal.Visible = False ' Hide calendar on load'
    End Sub

Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
        dateText.Text = dateCal.SelectedDate ' Update text box'
        dateCal.Visible = False              ' Hide calendar'
    End Sub

Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
        dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
    End Sub

Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
        dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
    End Sub

现在您可能会看到每次加载页面时,page_load事件都在调用并隐藏日历。

您应该在日历的ascx文件中设置Visible =“false”。或致电

dateCal.Visible = False 

仅在首次加载页面时(!IsPostback属性)

所以,在C#中它将是

protected void Page_Load(object sender, EventArgs args) {
   if (!IsPostback)
      dateCal.Visible = false;
}