简体   繁体   English

如何从asp.net中的静态方法访问Page对象

[英]How to access Page object from Static method in asp.net

I have a website, wherein i have a dropdownlist, where i need to show the price of the package selected from the dropdownlist, as i have lots of products listed on the page and everyone has the package dropdown 我有一个网站,其中有一个下拉列表,我需要显示从下拉列表中选择的软件包的价格,因为我在页面上列出了很多产品,每个人都有该软件包的下拉列表

i don't want to make postback on every selection of the package. 我不想对软件包的每个选择都进行回发。 so i would like to go with AJax jQuery implementation, i am using repeater control to show the list of products. 所以我想使用AJax jQuery实现,我正在使用中继器控件来显示产品列表。

Below is the webmethod function which i am using: 以下是我正在使用的webmethod函数:

[System.Web.Services.WebMethod]
    public static String FillLabel(int Index)
    {
    Page pa = HttpContext.Current.CurrentHandler as Page;
            Repeater homeRepeater = pa.FindControl("rptProducts") as Repeater;
         DropDownList drpUnit = homeRepeater.FindControl("drpQuantity") as DropDownList;
                 int Unit = int.Parse(drpUnit.SelectedItem.Text.Split(' ')[0].Trim());
                 HiddenField productId = (homeRepeater.Items[Index].FindControl("hdProductId") as HiddenField);
                 Package objPackage = new Package();
                 objPackage.ProductId = Convert.ToInt32(productId.Value);
                 objPackage.TownId = Globals.DefaultTown;
                 Label mrp = (homeRepeater.Items[Index].FindControl("lblMRP") as Label);
                 Label ourPrice = (homeRepeater.Items[Index].FindControl("lblOurPrice") as Label);
                 Label discount = (homeRepeater.Items[Index].FindControl("lblDiscount") as Label);
                 double discountPercent = Convert.ToDouble(objPackage.GetProductPackages().Find(item => item.Unit == Unit && item.ProductsInfo.ProductID == objPackage.ProductId).Discount);
                 string mrpVal = objPackage.GetProductPackages().Find(item => item.Unit == Unit && item.ProductsInfo.ProductID == objPackage.ProductId).MaximumRetailPrice.ToString();
                 string price = objPackage.GetProductPackages().Find(item => item.Unit == Unit && item.ProductsInfo.ProductID == objPackage.ProductId).SabkaSupermarketPrice.ToString();
                 mrp.Text = mrpVal;
                 ourPrice.Text = price;
                 mrp.Visible = (mrpVal != price);
                 if (discountPercent > 0)
                 {
                     discount.Visible = true;
                     discount.Text = objPackage.GetProductPackages().Find(item => item.Unit == Unit && item.ProductsInfo.ProductID == objPackage.ProductId).Discount.ToString() + "%<br/> OFF";
                 }
                 else
                 {
                     discount.Visible = false;
                 }
       return String.Empty;
}

Now my problem is i am unable to find repeater control as the function is static and i can't access the page object to find repeater control. 现在我的问题是我无法找到转发器控件,因为该函数是静态的,并且我无法访问页面对象来查找转发器控件。

Can anyone tell me how shall i access the repeater control in the static method? 谁能告诉我如何使用静态方法访问转发器控件?

i am pretty sure you dont have a Page object, nor its controls. 我很确定您没有Page对象或其控件。 your "current handler" is an ajax request. 您的“当前处理程序”是ajax请求。

if you must do it this way then you should use the AjaxPanel control which creates a full page cycle, meaning pageload and everything, then your AjaxPanel's method but the client itself feels a change as if it was an ajax request only. 如果必须这样做,则应使用AjaxPanel控件创建一个完整的页面周期,这意味着页面加载和所有内容,然后使用AjaxPanel的方法,但客户端本身会感觉到变化,就好像它只是一个ajax请求一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM