1.
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
student= new Student(100000);
txtName.Text=student.Name;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student.Name=txtName.Text;
student.Update();
}
}
//如果是这样 就会提示: 未将对象引用设置到对象的实例。 2.
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
student= new Student(100000);
txtName.Text=student.Name;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student.Name=txtName.Text;
student.Update();
}
}//不会提示 未将对象引用设置到对象的实例。 但更新不了
3.
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
student= new Student(100000);
txtName.Text=student.Name;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student= new Student(100000);
student.Name=txtName.Text;
student.Update();
}
}//正常运行了请解释这3种情况的原因
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
student= new Student(100000);
txtName.Text=student.Name;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student.Name=txtName.Text;
student.Update();
}
}
//如果是这样 就会提示: 未将对象引用设置到对象的实例。 2.
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
student= new Student(100000);
txtName.Text=student.Name;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student.Name=txtName.Text;
student.Update();
}
}//不会提示 未将对象引用设置到对象的实例。 但更新不了
3.
public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
student= new Student(100000);
txtName.Text=student.Name;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student= new Student(100000);
student.Name=txtName.Text;
student.Update();
}
}//正常运行了请解释这3种情况的原因
解决方案 »
- 页面加载时的等待效果
- facebox关闭事件
- asp.net 远程主机强迫关闭了一个现有的连接。
- GridView代码实现编辑功能出错,求教。
- 文件上传的问题
- 为什么javascript中parseFloat("32.22")+parseFloat("2.37")=34.58999999999999600000000000000000000000000000000000000000000000000000
- 求教:水晶报表问题
- Studio.NET中可以很好的调试ASP开发的Web应用吗?
- 如何写正则表达式:在SQL串中要么不出现这三个'、%、_字符要么成双出现。
- 请各位救急:如何让一个Web窗体启动完毕之后使它的一个文本框获得焦点?
- VS中安装AJax
- 请问如何让自定义控件大小随页面变化而变化?
另,建议lz看下.net相关的页面生存周期
2.其实已经更新了,那为什么更新不了呢,因为在点击按钮的时候进入了page_load,并且你又把txtName.Text=student.Name;等于把旧的数据又写回去了,你的改变没有奏效,然后你再提交的时候,数据是和数据库里一样的,所以更新不了
3.少掉了步骤2中的错误情况,又在后面重新提取了Student,当然就OK了。但是写法仍然不好。就你这个写法稍作改进:public partial class Student : System.Web.UI.Page
{
protected Student student; protected void Page_Load(object sender, EventArgs e)
{
student= new Student(100000);
if (!IsPostBack)
{
txtName.Text=student.Name;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
student.Name=txtName.Text;
student.Update();
}
}
1、当你发出请求到页面Render到客户端,一个页面完成了它的生命周期,并在服务端释放了他的所有资源,例如当页面呈现(render)完后,你的 Student就会释放掉(就不从在了)。2、那么页面在什么时候执行if(!IsPostBack){ }中的内容呢?并不是每次都执行。当第一次请求页面时会执行if(!IsPostBack){ }之中的代码。这个就是我们通常所说的回发和回传的问题。