简体   繁体   中英

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.

Below is the webmethod function which i am using:

[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. your "current handler" is an ajax request.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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