有一个管理页面
在TextBox上面显示一些通知的信息,比如标题,内容
有一些TextBox的内容为空
现在编辑的时候可能把一些空的TextBox填写值
然后保存
但是这个时候我调试发现这些空的TextBox的值还是空的
这么回事阿?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;using System.Data.SqlClient;
using Abo.Data;namespace abo.admin
{
/// <summary>
/// Summary description for edit.
/// </summary>
public class edit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label fgdf;
protected System.Web.UI.WebControls.TextBox title;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox content;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox reply;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList sort;
protected System.Web.UI.WebControls.CheckBox IsEssential;
public long id;
protected System.Web.UI.WebControls.Label name;
protected System.Web.UI.WebControls.Label fadfdasf;
protected System.Web.UI.WebControls.Label time;
public DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
if(Request["id"] == null||Session["user"] == null)
{
Response.Redirect("index.aspx");
}
}

this.IsEssential.Checked = true;
// Put user code to initialize the page here
id = Convert.ToInt64(Request["id"]);
Question objQuestion = new Question();
ds = objQuestion.GetSingleQuestion(id);
name.Text = ds.Tables[0].Rows[0][3].ToString();
if(ds.Tables[0].Rows[0][13].ToString().Trim() == "电脑类")
{
this.sort.SelectedIndex = 1;
}
else if(ds.Tables[0].Rows[0][13].ToString().Trim() == "数码影像类")
{
this.sort.SelectedIndex = 2;
}
else if(ds.Tables[0].Rows[0][13].ToString().Trim() == "数字音乐类")
{
this.sort.SelectedIndex = 3;
}
else if(ds.Tables[0].Rows[0][13].ToString().Trim() == "笔记本类")
{
this.sort.SelectedIndex = 4;
}
this.title.Text = ds.Tables[0].Rows[0][6].ToString();
this.content.Text = ds.Tables[0].Rows[0][8].ToString();
this.reply.Text = ds.Tables[0].Rows[0][9].ToString();
this.time.Text = ds.Tables[0].Rows[0][10].ToString();
if(ds.Tables[0].Rows[0][11].ToString() == "False")
{
this.IsEssential.Checked = false;
}
else if(ds.Tables[0].Rows[0][11].ToString() == "True")
{
this.IsEssential.Checked = true;
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion string type="";
bool Essential;

private void Button1_Click(object sender, System.EventArgs e)
{
    Expert objExpert = new Expert();

if(IsPostBack)
{
if(IsEssential.Checked == false)
{
Essential = false;
}
else
{
Essential = true;
}
if(this.sort.SelectedIndex == 0)
{
Response.Write("<script>alert('请选择类别!')</script>");
}
else if(this.sort.SelectedIndex == 1)
{
type = "电脑类";
}
else if(this.sort.SelectedIndex == 2)
{
type = "数码影像类";
}
else if(this.sort.SelectedIndex == 3)
{
type = "数字音乐类";
}
else if(this.sort.SelectedIndex == 4)
{
type = "笔记本类";
}
if(objExpert.QuestionEdit(id,this.name.Text.ToString(),this.title.Text.ToString(),this.content.Text.ToString(),this.reply.Text.ToString(),Essential,type,Session["user"].ToString()))
{
if(this.reply.Text.ToString().Trim()!=""&&ds.Tables[0].Rows[0][7].ToString()!="")
{
try
{
objExpert.SendMail(Session["user"].ToString(),ds.Tables[0].Rows[0][7].ToString().Trim(),this.reply.Text.ToString());
}
catch
{
Response.Write("<script>alert'邮件发送失败!'</script>");
}
Response.Redirect("expert.aspx");
}
}
else
{
Response.Write("编辑/回复失败!");
}
}
}
}
}

解决方案 »

  1.   

    程序有问题,因为你点击按钮以后还是会执行Page_Load()内的程序,把textbox的值重新赋了一遍,
    在onclick里面再做判断是不行的。
    应该将赋值的代码加到if(!Page.IsPostBack)的判断里面
      

  2.   

    楼主同一个问题问了两次啊,if(!Page.IsPostBack)一定要用好这个!
      

  3.   

    if(!Page.IsPostBack)一定要用好这个!
      

  4.   

    Page.IsPostBack表示不是第一次加载页面
    !Page.IsPostBack表示第一次加载页面
      

  5.   

    if(!Page.IsPostBack)里面写同样的问题,问了两次啊!
      

  6.   

    这个问题这样解决,去取request集合就行了!
    如:requeat.form("TB1")
    就OK了