-2

私はこのコードを持っています

if (PreviousPage.IsPostBack)
    {
        if (Request.Form["username"] == ConfigurationManager.AppSettings["username"] && Request.Form["password"] == ConfigurationManager.AppSettings["password"])
        {
            Session["username"] = Request.Form["username"];
            using (var context = new mallEntities())
            {
                var countProducts = (from p in context.Products
                                     select p).Count();

                var countStores = (from p in context.Stores
                                   select p).Count();

                var countCategories = (from p in context.Categories
                                       select p).Count();

                Label3.Text = countProducts.ToString();
                Label2.Text = countStores.ToString();
                Label1.Text = countCategories.ToString();
            }
        }
        else
        {
            Response.Redirect("Default.aspx?invaild=true");
        }
    } else if(Session["username"] == null)
    {
        Response.Redirect("Default.aspx?session=false");
    }

そして私はこのエラーメッセージを受け取りました:

Object reference not set to an instance of an object

PreviousPage.IsPostBack

なんで?

何が問題ですか?

4

1 に答える 1

4

なぜ使っているのかわからないPreviousPage.IsPosbackので、間違いだと思います。あなたは使用する必要がありますPage.IsPostback

本当に使用したい場合PreviousPage(私は困惑します)、ページに直接アクセスするとnullになる可能性があることを考慮に入れてください。

転送方法またはクロスページ投稿を使用して、あるASP.NETページから別のページに処理を転送する場合、元のページには、宛先ページに必要となる可能性のある要求情報が含まれます。その情報にアクセスするには、PreviousPageプロパティを使用します

ユーザーがサーバーから直接そのページを要求した場合、PreviousPageプロパティはnull参照(Visual BasicではNothing)です。

于 2012-05-18T18:39:28.013 に答える