Asp.net 从类库绑定转发器

Asp.net 从类库绑定转发器,asp.net,web-services,e-commerce,asprepeater,Asp.net,Web Services,E Commerce,Asprepeater,我有一个电子商务网站,用户可以将产品添加到购物车中,然后继续结账。我在主页上有一个购物车的通用显示,这样它在所有页面上都是可见的 现在在产品页面上,当用户单击“添加到购物车”按钮时,它应该用产品的数量实时更新购物车菜单。我在产品页面上为addtocart按钮使用ajax函数,ajax函数驻留在一个单独的类库项目中 我可以访问repeater控件并从单独的类库中的webmethod重新绑定它吗 这是我的密码: 母版页: <asp:Repeater ID="rptShoppingCart" r

我有一个电子商务网站,用户可以将产品添加到购物车中,然后继续结账。我在主页上有一个购物车的通用显示,这样它在所有页面上都是可见的

现在在产品页面上,当用户单击“添加到购物车”按钮时,它应该用产品的数量实时更新购物车菜单。我在产品页面上为addtocart按钮使用ajax函数,ajax函数驻留在一个单独的类库项目中

我可以访问repeater控件并从单独的类库中的webmethod重新绑定它吗

这是我的密码: 母版页:

<asp:Repeater ID="rptShoppingCart" runat="server">
    <HeaderTemplate>
        <table class="table">
    </HeaderTemplate>
    <ItemTemplate>
        <asp:HiddenField ID="hdnMRP" runat="server" Value='<%# double.Parse(Eval("MaximumRetailPrice").ToString()) * int.Parse(Eval("Quantity").ToString()) %>' />
        <tr>
            <td><asp:Image ID="imgProduct" Width="75" runat="server" ImageUrl='<%# Eval("Image") %>' /></td>
            <td>
                <div class="col-md-12"><%# Eval("Name") %></div>
                <div class="col-md-12">$<%# double.Parse(Eval("OurPrice").ToString()).ToString("N") %> X <%# Eval("Quantity") %> = $<asp:Label ID="lblTotal" runat="server" Text='<%# (double.Parse(Eval("OurPrice").ToString()) * int.Parse(Eval("Quantity").ToString())).ToString("N") %>'></asp:Label></div>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
        <table width="100%">
            <tr style="border-top: 1px solid #e4e7ea;">
                <td style="float:right;"><h2>$<%# SumShoppingValue().ToString("N") %></h2></td>
            </tr>
            <tr>
                <td style="float:right;"><%# SumShoppingSavings() %></td>
            </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>
我的内容页继承自作为类库的webservice类,而我的webservice继承自UI.page

公共类ProductPage:WebMethodLibrary
//这是我的内容页类声明

Page currentPage = new Page(); // I created this object, because my webmethod is a static method, hence i cannot use the Master.FindControl, and i am sure, my issue resides here only. Not sure what object need to be created.
Repeater rptShoppingCart = currentPage.Master.FindControl("rptShoppingCart") as Repeater;
rptShoppingCart.DataSource = shoppingDetails.GetShoppingCart();
rptShoppingCart.DataBind();
网络服务:
公共类WebMethodLibrary:Page
//这是我的webservice类声明

Page currentPage = new Page(); // I created this object, because my webmethod is a static method, hence i cannot use the Master.FindControl, and i am sure, my issue resides here only. Not sure what object need to be created.
Repeater rptShoppingCart = currentPage.Master.FindControl("rptShoppingCart") as Repeater;
rptShoppingCart.DataSource = shoppingDetails.GetShoppingCart();
rptShoppingCart.DataBind();

您可以在另一个类中的静态方法中执行此操作

Page currentPage = HttpContext.Current.CurrentHandler as Page;

Repeater rptShoppingCart = currentPage.Master.FindControl("rptShoppingCart") as Repeater;
rptShoppingCart.DataSource = shoppingDetails.GetShoppingCart();
rptShoppingCart.DataBind();
但是你的问题并不完全清楚,但是如果你使用一个对webmethod的Ajax调用,并试图找到不起作用的转发器。webmethod无权访问页面对象

因此,如果您进行ajax调用来填充转发器,您将需要一种显示购物车项目的纯JavaScript/jQuery方式