如何初始化asp.net页面

时间:2016-02-18 06:52:51

标签: asp.net vb.net

我在Page_Init中创建了大约50个ImageButton但是为每个按钮click调用了Page_Init。每次单击任何按钮,页面将再次转到首页。如何初始化asp页面?

代码: -

Dim isInitialized As Boolean = False
Dim calShipping As Calendar
Dim ibnCalendar As ImageButton
Dim lblShipping As Label
Dim pnlMain As Panel
Dim tblMain As Table
Dim txtShipping As TextBox

Private Sub ScheduleDetail_Init(sender As Object, e As EventArgs) Handles Me.Init
    Page.MaintainScrollPositionOnPostBack = True

    If (isInitialized = False) Then
        InitialDivMain()
        isInitialized = True
    End If
End Sub

Private Sub ScheduleDetail_Load(sender As Object, e As EventArgs) Handles Me.Load
    Page.MaintainScrollPositionOnPostBack = True
End Sub

Protected Sub calShipping_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
    calShipping.Visible = False
    txtShipping.Text = calShipping.SelectedDate.ToString("dd-MM-yyyy")
End Sub

Protected Sub ibnCalendar_Click(ByVal sender As Object, ByVal e As EventArgs)
    calShipping.Visible = True
End Sub

Private Sub InitialDivMain()
    Dim row As TableRow
    Dim cell As TableCell

    calShipping = New Calendar
    calShipping.Visible = False

    lblShipping = New Label

    txtShipping = New TextBox

    pnlMain = New Panel
    tblMain = New Table

    row = New TableRow
    row.VerticalAlign = VerticalAlign.Top
    cell = New TableCell
    cell.Controls.Add(lblShipping)
    row.Cells.Add(cell)
    cell = New TableCell
    cell.Controls.Add(txtShipping)
    row.Cells.Add(cell)
    cell = New TableCell
    ibnCalendar = New ImageButton
    ibnCalendar.ImageUrl = "~/image/calendar.png"
    AddHandler ibnCalendar.Click, AddressOf ibnCalendar_Click
    cell.Controls.Add(ibnCalendar)
    row.Cells.Add(cell)
    cell = New TableCell
    cell.Controls.Add(calShipping)
    row.Cells.Add(cell)
    tblMain.Rows.Add(row)
    pnlMain.Controls.Add(tblMain)
    divMain.Controls.Add(pnlMain)
End Sub

想法是单击图像按钮以在文本框中显示日历,用户选择日期和显示日期。每次单击图像按钮或单击日历,每次单击后,页面会自动滚动回到首页。

1 个答案:

答案 0 :(得分:0)

虽然你的问题可以更清楚,但我相信你说的是你的页面滚动位置会在每次回发时重置?

如果是这种情况,此代码可以帮助您。

protected void Page_Load(object sender, EventArgs e)
{
    Page.MaintainScrollPositionOnPostBack = true;
}