2

I'm create around 50 ImageButton in Page_Init but Page_Init get called for every button click. Every time click any button, the page will go to top page again. How to initialize asp page?

code: -

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

Idea is click an image button to display calendar, user select date and display date in textbox. Every time click the image button or click calendar, after each click, the page auto scroll back to top page again.

Community
  • 1
  • 1

1 Answers1

0

Although your question could be clearer I believe you are talking about the fact that your page-scroll position resets on every postback?

If that is the case, this code should help you out.

protected void Page_Load(object sender, EventArgs e)
{
    Page.MaintainScrollPositionOnPostBack = true;
}
Kaspar Kjeldsen
  • 936
  • 1
  • 13
  • 30
  • thanks @kaspar understand my question, but still reset page-scroll position. did put the code at Page_Init and Page_Load –  Feb 19 '16 at 00:35