跨请求保留变量

发布于 2024-12-28 10:25:05 字数 750 浏览 3 评论 0原文

我意识到这可能是一个简单的问题,但我是 ASP.net (C#) 的新手,并且在传递通过 LINQ-to- 从 sql 数据库获取的“Ride”对象的方法时遇到问题实体声明。我尝试将另一个全局变量设置为 Ride.identity 属性的值(很长),但在下一个方法中,当我尝试使用该值时,新值尚未保留。有什么想法吗?如果我缺少一些重新初始化该变量的回发,有没有办法保存它?谢谢。

private void displayRide(Ride ride, int carNum)
{
    if (ride != null) 
    {
        ride.AssignedCar = carNum;
        ride.Status = "EnRoute";
        id_ridePendingConfirm = ride.identity; //<----THE PROBLEM IS HERE!
        myEntities.SaveChanges();

        RideToAssignDV.DataSource = new List<Ride> {ride};
        RideToAssignDV.DataBind();                    
    } 
    else 
    {
        //TODO: Redirect to error.aspx
        RideToAssignDV.DataSource = null;
        RideToAssignDV.DataBind(); 
    }
}

I realize this may be a simple problem, but I am new to ASP.net (C#) and am having an issue with a method in which I pass a 'Ride' object that was obtained from an sql database through a LINQ-to-entities statement. I attempt to set another global variable to the value of the Ride.identity attribute (which is a long), but in the next method, when I attempt to use this value, the new value has not persisted. Any thoughts? If there is some post-back that I am missing that reinitializes this variable, is there a way to save it? Thanks.

private void displayRide(Ride ride, int carNum)
{
    if (ride != null) 
    {
        ride.AssignedCar = carNum;
        ride.Status = "EnRoute";
        id_ridePendingConfirm = ride.identity; //<----THE PROBLEM IS HERE!
        myEntities.SaveChanges();

        RideToAssignDV.DataSource = new List<Ride> {ride};
        RideToAssignDV.DataBind();                    
    } 
    else 
    {
        //TODO: Redirect to error.aspx
        RideToAssignDV.DataSource = null;
        RideToAssignDV.DataBind(); 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不回头走下去 2025-01-04 10:25:05

将值存储在 ViewState 中。例如:

ViewState["RideId"] = ride.identity;

当您在代码行中使用它时,您需要这样做:

id_ridePendingConfirm = (long)ViewState["RideId"];

但是要小心。由于 ViewState[key] 返回一个对象,因此您需要确保它不是空引用,否则您将收到 InvalidCastException。

我通常会告诉经验较少的同事创建一个受保护的属性,该属性将存储该值并将其保留在 ViewState 中,如下所示:

protected const string RideIdViewStateKey = "CurrentRideId";

protected long CurrentRideId
{
    get
    {
        object o = ViewState[RideIdViewStateKey];
        return (null == o)? -1 : (long)o;
    }

    set
    {
        ViewState[RideIdViewStateKey] = value;
    }
} 

然后在您的代码中执行以下操作:

// Assignment before postback so that you can preserve the state:
CurrentRideId = ride.identity;

// After postback in the method you have above:
id_ridePendingConfirm = CurrentRideId;

现在,因为我不知道您的代码在什么时候应该做什么没有可用的 Ride 身份,我决定使用 -1,但这取决于您的代码实际需要的内容。

我不建议您的场景使用会话状态,因为这显然需要在页面回发之间持续存在,而不是在用户会话的整个持续时间内持续存在。还要小心在 ViewState 中存储的信息量,因为它很容易被滥用。

有关 ViewState 和会话状态的详细信息,请参阅:

Store the value in ViewState. For example:

ViewState["RideId"] = ride.identity;

When you go and use it in the line in your code, you would need to do this:

id_ridePendingConfirm = (long)ViewState["RideId"];

But be careful. Since ViewState[key] returns an object, you will need to make sure it isn't a null reference, or else you'll receive an InvalidCastException.

I normally tell my my peers with less experience to create a protected property that will store this value and persist it in ViewState, as follows:

protected const string RideIdViewStateKey = "CurrentRideId";

protected long CurrentRideId
{
    get
    {
        object o = ViewState[RideIdViewStateKey];
        return (null == o)? -1 : (long)o;
    }

    set
    {
        ViewState[RideIdViewStateKey] = value;
    }
} 

Then in your code, do this:

// Assignment before postback so that you can preserve the state:
CurrentRideId = ride.identity;

// After postback in the method you have above:
id_ridePendingConfirm = CurrentRideId;

Now, since I don't know what your code should do when no Ride identity is available, I decided for -1, but it depends on what your code actually needs.

I do not recommend Session state for your scenario because this apparently needs to persist between page postbacks, not for the entire duration of the user's session. Also be careful with how much information you store in ViewState because it can easily be abused.

For more information on ViewState and Session State see:

初与友歌 2025-01-04 10:25:05

变量和字段不会在回发过程中持续存在。

您需要使用会话状态。

Variables and fields do not persist across postbacks.

You need to use session state.

百善笑为先 2025-01-04 10:25:05

请参阅此问题和我的答案了解多种方法在页面执行之间存储数据。简而言之,您需要将值存储在回发之间的某个位置。 ASP.NET(以及一般的.NET)提供了多种工具来执行此操作,但请求之间不会自动执行任何操作。该页面会随着每个请求而创建、执行和销毁。

See this question and my answer for multiple methods of storing data between executions of a page. In brief, you need to store the value somewhere between postbacks. ASP.NET (and .NET in general) provide a variety of tools for doing so, but nothing is done automatically between requests. The page is created, executed, and destroyed with every request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文