Click here to Skip to main content
15,888,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I Have a Page With 50 Types Of Products Taken In Datalist And For Each Product There I Had Put A Button "Add To Cart".
My Problem Is That When I Click On The Button "Add To Cart" Of A Product, Then The Page Is Scrolling To Top Position Always.
How To Stop The Scrolling Of A Page When Button Is Clicked..
Some One Help Me..Thanks In Advance...

The Designing Code I Had Used :

XML
<%@ Page Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" MaintainScrollPositionOnPostback="true"
    CodeFile="Products.aspx.cs" Inherits="Library" Title="Products Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <br />
     <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
        DataKeyField="ProductID"
        onselectedindexchanged="DataList1_SelectedIndexChanged" RepeatColumns="3" RepeatDirection="Horizontal"
    BorderColor="#336699" BorderStyle="None" BorderWidth="2px" Width="747px">
            <ItemTemplate>
                <%--ProductID:--%>
                <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' Visible="false"/>
                <br />
                <br />

                <asp:Label ID="ProductNameLabel" CssClass="class1" runat="server"
                    Text='<%# Eval("ProductName") %>' />
                <br /><br />
                <asp:Image ID="Image1" runat="server" CssClass="class1" ImageUrl='<%# Eval("ProductImage") %>' Height="100" Width="150"/><br />
                <asp:Label ID="ProductImageLabel" runat="server" Text='<%# Eval("ProductImage") %>' Visible="false" /><br />
                <br />
                <asp:Button ID="lnkSelect" runat="server" CommandName="Select" Text="Add To Cart" />
                Price:
                <asp:Label ID="ProductPriceLabel" runat="server" Text='<%# Eval("ProductPrice") %>' />

                <br />

            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [ProductID], [TypeOfImage], [ProductName], [ProductDescription], [ProductPrice], [ProductImage] FROM [tblImages] WHERE ([TypeOfImage] = @TypeOfImage) ORDER BY ProductName">
            <SelectParameters>
                <asp:ControlParameter ControlID="DataList1" DefaultValue="Products"
                    Name="TypeOfImage" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

</asp:Content>
Posted
Updated 1-Aug-13 1:30am
v4
Comments
Ankur\m/ 1-Aug-13 7:00am    
browser specific or all browsers?
CHAITANYA KIRAN KASANI 1-Aug-13 7:07am    
for all browsers
Ankur\m/ 1-Aug-13 7:38am    
Do you have any client script attached to the button?
Joezer BH 1-Aug-13 7:09am    
Are you using anchors in the page (<a href="#..."&gt; etc)?
CHAITANYA KIRAN KASANI 1-Aug-13 7:22am    
this is the code i had used

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
DataKeyField="ProductID"
onselectedindexchanged="DataList1_SelectedIndexChanged" RepeatColumns="3" RepeatDirection="Horizontal"
BorderColor="#336699" BorderStyle="None" BorderWidth="2px" Width="747px">
<itemtemplate>
<%--ProductID:--%>
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' Visible="false"/>
<br />

<br />

<asp:Label ID="ProductNameLabel" CssClass="class1" runat="server"
Text='<%# Eval("ProductName") %>' />
<br /><br />
<asp:Image ID="Image1" runat="server" CssClass="class1" ImageUrl='<%# Eval("ProductImage") %>' Height="100" Width="150"/><br />
<asp:Label ID="ProductImageLabel" runat="server" Text='<%# Eval("ProductImage") %>' Visible="false" /><br />
<br />
<asp:Button ID="lnkSelect" runat="server" CommandName="Select" Text="Add To Cart" />
Price:
<asp:Label ID="ProductPriceLabel" runat="server" Text='<%# Eval("ProductPrice") %>' />

<br />

<itemstyle horizontalalign="Center" verticalalign="Top">

I Got The Solution:

MaintainScrollPositionOnPostback Property is not working in Firefox and Chrome and provided a solution that works in all major browsers Internet Explorer IE, FireFox, Google Chrome, Apple Safari and Opera.

I refered The Below Link , It Gives The Code In Javascript That Works Well On MaintainScrollPositionOnPostback In All Browers
http://www.aspsnippets.com/Articles/ASPNet-MaintainScrollPositionOnPostback-not-working-in-Firefox-and-Chrome.aspx
 
Share this answer
 
I hope you are using # tag in href.

Use "javascript:void(0)" instead of "#"

Please check this.

http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0[^]
 
Share this answer
 
v2
Comments
CHAITANYA KIRAN KASANI 1-Aug-13 7:09am    
no i didnot give href, i had given
<asp:Button ID="lnkSelect" runat="server" CommandName="Select" Text="Add To Cart" />
HI,

If your page is aspx page then you can add the following attribute to your aspx page directive that will stop the scrolling of the page during the button click.

MaintainScrollPositionOnPostback="true"


This is the simplest way of doing this. Try this one for your requirement.

Thanks
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 1-Aug-13 7:16am    
i had added but same problem is coming
Thanks7872 1-Aug-13 7:20am    
show the code where you have added the above line.
[no name] 1-Aug-13 7:47am    
What is the problem you are facing here? Please post the problem too.
CHAITANYA KIRAN KASANI 1-Aug-13 7:30am    
pls see the code i had updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900