Asp.Net C中的Session Var问题# [英] Problem with Session Var in Asp.Net C#

查看:45
本文介绍了Asp.Net C中的Session Var问题#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我们的代码存在问题:



DataTable DataTable11 =(DataTable)Session [DataTableSession];

DataTable dtDemo = DataTable11;



if(DataTable11!= null)

{

DataTable11.Columns.Remove (Id);

}





删除2列DataTable var(DataTable11)和dtDemo),怎么可能这个???



我想删除DataTable11的Id列,而dtDemo不会删除任何内容。



谢谢你,对不起我的英语。

Hello, we have a problem with this code:

DataTable DataTable11 = (DataTable)Session["DataTableSession"];
DataTable dtDemo = DataTable11;

if (DataTable11 != null)
{
DataTable11.Columns.Remove("Id");
}


The column Id is delete for 2 DataTable var (DataTable11 and dtDemo), how is possible this???

I want to delete the Id Column for DataTable11 and the dtDemo doesn't delete nothing.

Thank you and sorry for my English.

推荐答案

你需要回过头几步,查看基础知识什么是变量,引用和实例。

此代码:

You need to go back a few steps and look up the basics on what a variable, a reference, and an instance are.
This code:
DataTable DataTable11 = (DataTable)Session["DataTableSession"];

创建数据Table11变量,并将会话中的DataTable实例的引用分配给它。

Creates the DataTable11 variable, and assigns a reference to the DataTable instance from the session to it.

DataTable dtDemo = DataTable11;

这会创建第二个变量名为dtDemo,从DataTable11 复制对DataTable实例的引用。

它不复制表:它复制引用,因此两个变量都指的是相同的宾语。你通过一个变量做的任何东西似乎已经完成了另一个变量,因为他们都在谈论相同的项目。



将电视视为DataTable,和两个遥控器作为变量。无论你使用哪种遥控器,它们都会改变同一台电视上的频道,因为它们都是指电视的同一个实例。



如果你想要有两个单独的DataTable,因此您可以更改一个而不影响另一个,然后您需要使用 DataTable.Copy [ ^ ]方法。

This creates a second variable called dtDemo, and copies the reference to the DataTable instance from DataTable11.
It does not copy the table: it copies the reference, so that both variables are referring to the same object. Anything you do via one variable appears to have been done to the other because they are both "talking about" the same item.

Think of a TV as the DataTable, and two remote controls as the variables. Regardless of which remote control you use, they both change the channel on the same TV, because they both refer to the same instance of a television.

If you want to have two separate DataTables so you can change one and not affect the other, then you need to copy the existing instance using the DataTable.Copy[^] method.


这篇关于Asp.Net C中的Session Var问题#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆