单从代码上讲:第一种方法是不论OperatorID是否等于0,都会执行:
                 Response.Write(TBOperatorName.Text);
第二中方法,只有在OperatorID不等于0时,才执行:
                 Response.Write(TBOperatorName.Text);这就是两个方法的区别

解决方案 »

  1.   

    你是不是有类似于
    if (OperatorID!=0)
    TBOperatorName.Text=""
    这种句子而你没注意?如果OperatorID==0
    Response.Write(TBOperatorName.Text);
    在第二种里不会执行,是不是给你造成了TBOperatorName.Text为空的假象?因为不知道你的层次,不好作猜测
      

  2.   

    我设断点跟踪了呀
    (OperatorID!=0)
    的情况肯定执行了
    绝对没有TBOperatorName.Text=""的语句我的代码简单的不能再简单了,我全贴出来
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace SM
    {
    /// <summary>
    /// OperaterEditor 的摘要说明。
    /// </summary>
    public class OperatorEditor : System.Web.UI.Page
    { int OperatorID;
    SqlConnection objConn;
    SqlDataReader objReader;
    SqlCommand objCommand;
    protected System.Web.UI.WebControls.TextBox TBOperatorName;
    protected System.Web.UI.WebControls.Button BTOK;
    protected System.Web.UI.WebControls.Button BTCancel;
    string sql;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"].ToString().Trim();
    objConn = new SqlConnection(ConnStr);
    if (Request["OperatorID"]!=null)
    {
    OperatorID = Convert.ToInt32(Request["OperatorID"]);
    ReadOperaterInfo(OperatorID);
    } } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.BTOK.Click += new System.EventHandler(this.BTOK_Click);
    this.BTCancel.Click += new System.EventHandler(this.BTCancel_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion //读取操作员的信息
    private void ReadOperaterInfo(int OperatorID)
    {
    sql = "select * from USERINFO_SM where USER_ID=" + OperatorID;
    objConn.Open();
    objCommand = new SqlCommand(sql,objConn);
    objReader = objCommand.ExecuteReader();
    while(objReader.Read())
    {
    TBOperatorName.Text = objReader.GetString(1).Trim();
    }
    objReader.Close();
    objConn.Close();
    Response.Write(OperatorID);
    } private void BTOK_Click(object sender, System.EventArgs e)
    {

    // Response.Write(TBOperatorName.Text);
    if (OperatorID==0)
    AddNewOperator();
    if (OperatorID!=0)
    {
    Response.Write(TBOperatorName.Text);
    // ModifyOperator(OperatorID);
    }
    } //添加操作员
    private void AddNewOperator()
    {
    } //修改操作员信息
    private void ModifyOperator(int OperatorID)
    {

    sql = "update USERINFO_SM set USER_NAME = @OperatorName where USER_ID = @UserID";
    objCommand = new SqlCommand(sql,objConn);

    objCommand.Parameters.Add(new SqlParameter("@OperatorName",SqlDbType.NVarChar,128));
    objCommand.Parameters["@OperatorName"].Value = TBOperatorName.Text; objCommand.Parameters.Add(new SqlParameter("@UserID",SqlDbType.Int));
    objCommand.Parameters["@UserID"].Value = OperatorID; try
    {
    objConn.Open();
    objCommand.ExecuteNonQuery();
    // Response.Write("<script>window.close()</script>");
    }
    catch(Exception ex)
    {
    // Response.Write("<srcipt language=javascript>alert("error");return false;</script>");
    Response.Write(ex.ToString());
    RegisterStartupScript("","<script>alert('error');</script>");
    }
    finally
    {
    objConn.Close();
    }
    } private void BTCancel_Click(object sender, System.EventArgs e)
    {
    Response.Write(TBOperatorName.Text);
    }
    }
    }