register dynamic javacript with RegisterStartupScript

Discussion in 'ASP .Net' started by Jason Chan, Nov 19, 2005.

  1. Jason Chan

    Jason Chan Guest

    I have a paging datalist which show a list of thumbnail.

    Above the datalist there is a dropdown to jump to different paging.
    On Page_load, I bind the datalist according to the current page.
    On the same time, I have to generate a dynamic javascript to load the image
    in the datalist to an javascript array

    So I use Page.RegisterStartupScript to insert the script just before the
    closing form tag.

    The code works fine for the first page, however when I jump to another page,
    from the source code, I found that the JavaScript keep showing the script of
    the first page. This make my page doesn't work properly

    That is, for example.

    page 1 of datalist load 5 images (1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg)
    the javascript of the page 1 is dynamic generated and registered
    img[0] = "1.jpg";img[1] = "2.jpg";img[2] = "3.jpg";img[3] = "4.jpg";img[4] =

    when I jump to page 2, it load another 5 images (6.jpg, 7.jpg, 8.jpg, 9.jpg,
    the javascript should look like
    img[0] = "6.jpg";img[1] = "7.jpg";img[2] = "8.jpg";img[3] = "9.jpg";img[4] =

    However, it just keep showing the first script, any idea?
    P.S using ASP.NET 2.0
    Jason Chan, Nov 19, 2005
    1. Advertisements

  2. Without seeing any code, it is not possible to determine the exact cause,
    but this is my theory:

    In your page_load you have something similar to:

    If not ispostback then
    end if


    And you have in a paging handler:

    On first load of the page, the page load will load the first page of data
    and bind the data, then will create the javascript for your page.

    On subsequent postbacks, the page load will run first, creating the
    javascript for the first page, then the event handler will fire, which
    loads the next page and binds it to the datalist.

    If this is the correct scenario, you will need to call the method that
    creates the javascript in your event handler so it is current with the
    data that has been loaded. The base problem I suspect is that you have
    your javascript generate call in the page load which is fired before the
    new page data is loaded.

    Hope this helps.

    700cb Development, Inc.
    ..NET utilities, developer tools,
    and enterprise solutions

    cbDevelopment, Dec 3, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.