GridView PageIndexChanged

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    GridView PageIndexChanged

    I want to call a JavaScript on PageIndexChange d event, how do I do that?

    Thank You


    Peter


  • Steven Cheng [MSFT]

    #2
    RE: GridView PageIndexChange d

    Hi Peter,

    As for executing some javascript code on pageIndexChange d event, would you
    provide some further information about your page scenario or requirement?

    So far based on my understanding, here are some possible approaches we can
    add client script behavior at Gridview's paging stage:

    1. You can use Gridview's PageIndexChaged event and use
    "ClientScript.R egisterStartupS cript" to emit some clientscript to
    client-side for execution(when the page's output is rendering in client
    browser).

    =======GridView page index changed======== ===
    protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
    {
    ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
    "page_index_scr ipt", "alert('pag e index changed!');", true);

    }
    =============== =====

    2. If you need to do some pre processing(such as right after the user
    change Gridview page), I think you may consider customize the Pager of the
    gridview. for example, you can define the pager yourself (such as put two
    "Move Next" and "Move Previous" buttons ) and you can add script functions
    for the two button's click event(one is to do some tasks you want
    client-side, another is postback to change page index).

    For custom paging, here is an example:

    #GridView Custom Paging


    If you have any other concerns or any other questions, welcome to post here
    for discussion.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.





    --------------------
    >From: "Peter" <czupet@nospam. nospam>
    >Subject: GridView PageIndexChange d
    >Date: Wed, 23 Jul 2008 14:42:07 -0500
    >
    >I want to call a JavaScript on PageIndexChange d event, how do I do that?
    >
    >Thank You
    >
    >
    >Peter
    >
    >
    >

    Comment

    • Peter

      #3
      Re: GridView PageIndexChange d

      The suggested code 'ClientScript.R egisterStartupS cript(this.GetT ype(), ' is
      not working nothing happens - when I click on #2 page in the GridView
      the event fires but alert never appears.


      I have JavaScript resizing code, when the window is resized I use JavaScript
      to resize the GridView so it is resized with the window. But when a user
      clicks on the pager the GridView resizes to its original size which does not
      fit into the window. So I want to call the client JavaScript after user
      clicks on the pager just like I do when the window is resized.



      "Steven Cheng [MSFT]" <stcheng@online .microsoft.comw rote in message
      news:a6OPm$T7IH A.3320@TK2MSFTN GHUB02.phx.gbl. ..
      Hi Peter,
      >
      As for executing some javascript code on pageIndexChange d event, would you
      provide some further information about your page scenario or requirement?
      >
      So far based on my understanding, here are some possible approaches we can
      add client script behavior at Gridview's paging stage:
      >
      1. You can use Gridview's PageIndexChaged event and use
      "ClientScript.R egisterStartupS cript" to emit some clientscript to
      client-side for execution(when the page's output is rendering in client
      browser).
      >
      =======GridView page index changed======== ===
      protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
      {
      ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
      "page_index_scr ipt", "alert('pag e index changed!');", true);
      >
      }
      =============== =====
      >
      2. If you need to do some pre processing(such as right after the user
      change Gridview page), I think you may consider customize the Pager of the
      gridview. for example, you can define the pager yourself (such as put two
      "Move Next" and "Move Previous" buttons ) and you can add script functions
      for the two button's click event(one is to do some tasks you want
      client-side, another is postback to change page index).
      >
      For custom paging, here is an example:
      >
      #GridView Custom Paging

      >
      If you have any other concerns or any other questions, welcome to post
      here
      for discussion.
      >
      Sincerely,
      >
      Steven Cheng
      >
      Microsoft MSDN Online Support Lead
      >
      >
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.
      >
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      ications.
      >
      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the community or a Microsoft Support
      Engineer within 1 business day is acceptable. Please note that each follow
      up response may take approximately 2 business days as the support
      professional working with you may need further investigation to reach the
      most efficient resolution. The offering is not appropriate for situations
      that require urgent, real-time or phone-based interactions or complex
      project analysis and dump analysis issues. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at
      http://msdn.microsoft.com/subscripti...t/default.aspx.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no
      rights.
      >
      >
      >
      >
      >
      --------------------
      >>From: "Peter" <czupet@nospam. nospam>
      >>Subject: GridView PageIndexChange d
      >>Date: Wed, 23 Jul 2008 14:42:07 -0500
      >
      >>
      >>I want to call a JavaScript on PageIndexChange d event, how do I do that?
      >>
      >>Thank You
      >>
      >>
      >>Peter
      >>
      >>
      >>
      >

      Comment

      • Steven Cheng [MSFT]

        #4
        Re: GridView PageIndexChange d

        Thanks for your prompt reply Peter,

        Now I got that you want to do some resizing work on the GridView after page
        index changes, then I think the first approach I mentioned earlier (about
        emiting script in "PageIndexChang ed" event should work.

        As you said that the "alert" didn't popup, I think there might be something
        else cause this. Is your page a standard ASP.NET page, or have you used any
        AJAX feature such as updatepanel? There might be some problem with some
        GridView events in updatepanel.

        Here is my complete page code for the test(a standard simple aspx page):
        =============== =======
        <form id="form1" runat="server">
        <div>

        <asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue"
        AutoGenerateCol umns="False" DataKeyNames="i d"
        DataSourceID="S qlDataSource1"
        onpageindexchan ged="GridView1_ PageIndexChange d"
        onprerender="Gr idView1_PreRend er" PageSize="2">
        <Columns>
        <asp:BoundFie ld DataField="id" HeaderText="id"
        ReadOnly="True"
        SortExpression= "id" />
        <asp:BoundFie ld DataField="name " HeaderText="nam e"
        SortExpression= "name" />
        <asp:BoundFie ld DataField="age" HeaderText="age "
        SortExpression= "age" />
        </Columns>
        </asp:GridView>

        </div>
        <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
        ConnectionStrin g="<%$ ConnectionStrin gs:testdbConnec tionString %>"
        SelectCommand=" SELECT [id], [name], [age] FROM [persons]">
        </asp:SqlDataSour ce>
        </form>
        =============== =============

        ========code behind========= ====
        protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
        {
        ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
        "page_index_scr ipt", "alert('pag e index changed!');", true);
        }
        =============== ==

        Also, you can just define a javascript function statically in the aspx
        template and reference it in the "RegisterStartu pScript" function's
        parameter. If necessary, I can send you the web project contains the page
        for testing. Please feel free to let me know if there is anything else
        different.

        Sincerely,

        Steven Cheng

        Microsoft MSDN Online Support Lead

        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

        ications.

        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.


        --------------------
        >From: "Peter" <czupet@nospam. nospam>
        >References: <uar#xwP7IHA.23 36@TK2MSFTNGP03 .phx.gbl>
        <a6OPm$T7IHA.33 20@TK2MSFTNGHUB 02.phx.gbl>
        >Subject: Re: GridView PageIndexChange d
        >Date: Thu, 24 Jul 2008 00:13:16 -0500
        >The suggested code 'ClientScript.R egisterStartupS cript(this.GetT ype(), '
        is
        >not working nothing happens - when I click on #2 page in the GridView
        >the event fires but alert never appears.
        >
        >
        >I have JavaScript resizing code, when the window is resized I use
        JavaScript
        >to resize the GridView so it is resized with the window. But when a user
        >clicks on the pager the GridView resizes to its original size which does
        not
        >fit into the window. So I want to call the client JavaScript after user
        >clicks on the pager just like I do when the window is resized.
        >
        >
        >
        >"Steven Cheng [MSFT]" <stcheng@online .microsoft.comw rote in message
        >news:a6OPm$T7I HA.3320@TK2MSFT NGHUB02.phx.gbl ...
        >Hi Peter,
        >>
        >As for executing some javascript code on pageIndexChange d event, would
        you
        >provide some further information about your page scenario or requirement?
        >>
        >So far based on my understanding, here are some possible approaches we
        can
        >add client script behavior at Gridview's paging stage:
        >>
        >1. You can use Gridview's PageIndexChaged event and use
        >"ClientScript. RegisterStartup Script" to emit some clientscript to
        >client-side for execution(when the page's output is rendering in client
        >browser).
        >>
        >=======GridVie w page index changed======== ===
        >protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
        > {
        > ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
        >"page_index_sc ript", "alert('pag e index changed!');", true);
        >>
        > }
        >============== ======
        >>
        >2. If you need to do some pre processing(such as right after the user
        >change Gridview page), I think you may consider customize the Pager of
        the
        >gridview. for example, you can define the pager yourself (such as put two
        >"Move Next" and "Move Previous" buttons ) and you can add script
        functions
        >for the two button's click event(one is to do some tasks you want
        >client-side, another is postback to change page index).
        >>
        >For custom paging, here is an example:
        >>
        >#GridView Custom Paging
        >http://www.codeproject.com/KB/webfor...tomPaging.aspx
        >>
        >If you have any other concerns or any other questions, welcome to post
        >here
        >for discussion.
        >>
        >Sincerely,
        >>
        >Steven Cheng
        >>
        >Microsoft MSDN Online Support Lead
        >>
        >>
        >Delighting our customers is our #1 priority. We welcome your comments and
        >suggestions about how we can improve the support we provide to you.
        Please
        >feel free to let my manager know what you think of the level of service
        >provided. You can send feedback directly to my manager at:
        >msdnmg@microsof t.com.
        >>
        >============== =============== =============== ======
        >Get notification to my posts through email? Please refer to
        >>
        http://msdn.microsoft.com/subscripti...ult.aspx#notif
        >

        Comment

        • Peter

          #5
          Re: GridView PageIndexChange d

          Sorry I did not mention it before, yes I am using AJAX update panel.

          And that's probably why any "JavaScript " code in the
          GridView1_PageI ndexChanged event: does not work



          I was looking for something like this:
          GridView1.Attri butes.Add("Page IndexChange", "ResizeGrid();" );

          but this code does not work either, no errors and nothing happens.


          "Steven Cheng [MSFT]" <stcheng@online .microsoft.comw rote in message
          news:ghqajJV7IH A.5288@TK2MSFTN GHUB02.phx.gbl. ..
          Thanks for your prompt reply Peter,
          >
          Now I got that you want to do some resizing work on the GridView after
          page
          index changes, then I think the first approach I mentioned earlier (about
          emiting script in "PageIndexChang ed" event should work.
          >
          As you said that the "alert" didn't popup, I think there might be
          something
          else cause this. Is your page a standard ASP.NET page, or have you used
          any
          AJAX feature such as updatepanel? There might be some problem with some
          GridView events in updatepanel.
          >
          Here is my complete page code for the test(a standard simple aspx page):
          =============== =======
          <form id="form1" runat="server">
          <div>
          >
          <asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue"
          AutoGenerateCol umns="False" DataKeyNames="i d"
          DataSourceID="S qlDataSource1"
          onpageindexchan ged="GridView1_ PageIndexChange d"
          onprerender="Gr idView1_PreRend er" PageSize="2">
          <Columns>
          <asp:BoundFie ld DataField="id" HeaderText="id"
          ReadOnly="True"
          SortExpression= "id" />
          <asp:BoundFie ld DataField="name " HeaderText="nam e"
          SortExpression= "name" />
          <asp:BoundFie ld DataField="age" HeaderText="age "
          SortExpression= "age" />
          </Columns>
          </asp:GridView>
          >
          </div>
          <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
          ConnectionStrin g="<%$ ConnectionStrin gs:testdbConnec tionString %>"
          SelectCommand=" SELECT [id], [name], [age] FROM [persons]">
          </asp:SqlDataSour ce>
          </form>
          =============== =============
          >
          ========code behind========= ====
          protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
          {
          ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
          "page_index_scr ipt", "alert('pag e index changed!');", true);
          }
          =============== ==
          >
          Also, you can just define a javascript function statically in the aspx
          template and reference it in the "RegisterStartu pScript" function's
          parameter. If necessary, I can send you the web project contains the page
          for testing. Please feel free to let me know if there is anything else
          different.
          >
          Sincerely,
          >
          Steven Cheng
          >
          Microsoft MSDN Online Support Lead
          >
          Delighting our customers is our #1 priority. We welcome your comments and
          suggestions about how we can improve the support we provide to you. Please
          feel free to let my manager know what you think of the level of service
          provided. You can send feedback directly to my manager at:
          msdnmg@microsof t.com.
          >
          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

          ications.
          >
          =============== =============== =============== =====
          This posting is provided "AS IS" with no warranties, and confers no
          rights.
          >
          >
          --------------------
          >>From: "Peter" <czupet@nospam. nospam>
          >>References: <uar#xwP7IHA.23 36@TK2MSFTNGP03 .phx.gbl>
          <a6OPm$T7IHA.33 20@TK2MSFTNGHUB 02.phx.gbl>
          >>Subject: Re: GridView PageIndexChange d
          >>Date: Thu, 24 Jul 2008 00:13:16 -0500
          >
          >>The suggested code 'ClientScript.R egisterStartupS cript(this.GetT ype(), '
          is
          >>not working nothing happens - when I click on #2 page in the GridView
          >>the event fires but alert never appears.
          >>
          >>
          >>I have JavaScript resizing code, when the window is resized I use
          JavaScript
          >>to resize the GridView so it is resized with the window. But when a user
          >>clicks on the pager the GridView resizes to its original size which does
          not
          >>fit into the window. So I want to call the client JavaScript after user
          >>clicks on the pager just like I do when the window is resized.
          >>
          >>
          >>
          >>"Steven Cheng [MSFT]" <stcheng@online .microsoft.comw rote in message
          >>news:a6OPm$T7 IHA.3320@TK2MSF TNGHUB02.phx.gb l...
          >>Hi Peter,
          >>>
          >>As for executing some javascript code on pageIndexChange d event, would
          you
          >>provide some further information about your page scenario or
          >>requirement ?
          >>>
          >>So far based on my understanding, here are some possible approaches we
          can
          >>add client script behavior at Gridview's paging stage:
          >>>
          >>1. You can use Gridview's PageIndexChaged event and use
          >>"ClientScript .RegisterStartu pScript" to emit some clientscript to
          >>client-side for execution(when the page's output is rendering in client
          >>browser).
          >>>
          >>=======GridVi ew page index changed======== ===
          >>protected void GridView1_PageI ndexChanged(obj ect sender, EventArgs e)
          >> {
          >> ClientScript.Re gisterStartupSc ript(this.GetTy pe(),
          >>"page_index_s cript", "alert('pag e index changed!');", true);
          >>>
          >> }
          >>============= =======
          >>>
          >>2. If you need to do some pre processing(such as right after the user
          >>change Gridview page), I think you may consider customize the Pager of
          the
          >>gridview. for example, you can define the pager yourself (such as put
          >>two
          >>"Move Next" and "Move Previous" buttons ) and you can add script
          functions
          >>for the two button's click event(one is to do some tasks you want
          >>client-side, another is postback to change page index).
          >>>
          >>For custom paging, here is an example:
          >>>
          >>#GridView Custom Paging
          >>http://www.codeproject.com/KB/webfor...tomPaging.aspx
          >>>
          >>If you have any other concerns or any other questions, welcome to post
          >>here
          >>for discussion.
          >>>
          >>Sincerely,
          >>>
          >>Steven Cheng
          >>>
          >>Microsoft MSDN Online Support Lead
          >>>
          >>>
          >>Delighting our customers is our #1 priority. We welcome your comments
          >>and
          >>suggestions about how we can improve the support we provide to you.
          Please
          >>feel free to let my manager know what you think of the level of service
          >>provided. You can send feedback directly to my manager at:
          >>msdnmg@microsof t.com.
          >>>
          >>============= =============== =============== =======
          >>Get notification to my posts through email? Please refer to
          >>>
          http://msdn.microsoft.com/subscripti...ult.aspx#notif
          >>
          >

          Comment

          Working...