请教一个问题。我用的ASP.NET(C#)。我在网页中有个面板Panel控件,在面板中我插入了一个4行1列的表格,在最后一行单元格中有2个按扭一个是添加按扭(名:btnInsert),一个是修改按扭(名:Button2)在第一个单元格中我有个文本框,文本框(名为:txtNum)的后面是一个自定义验证控件用来验证该文本框的值是否是数据库中已经存在的,如果存在,在单击添加按扭的时候就不能插入到数据库中,如果不存在,就可以添加    。修改按扭是用的同一个验证控件来验证同一个文本框,当文本框的值是数据库中存在的时候,就可以修改其他文本框的值(这里我就不把其他文本框列举出来了)。这时问题就出来了,写好上面的代码后,运行,在各个文本框中添入正确的数据,而且在txtNum中添入数据库中没有的值时,我单击添加按扭,就会成功。而当我在各个文本框中添入正确的数据,在txtNum文本框中填入数据库中已有那个值的时候,单击修改按钮就会触发第一个添加按钮中设置的那个验证控件的处理方式,而不是修改按钮设置的对应的处理方式,这样导致了修改每次都不能成功。大概的整体代码如下,代码中我调用的另一个类中的方法中的代码是绝对没有错的: private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.txtContent.TextChanged += new System.EventHandler(this.txtContent_TextChanged);
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void txtContent_TextChanged(object sender, System.EventArgs e)
{

}

private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string sid=this.txtNum.Text;
if(DB.inspect(sid))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
} private void btnInsert_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
News news=new News();
news.nid=this.txtType.Text;
news.nyid=this.txtNum.Text;
news.ntitle=this.txtTitle.Text;
news.nnewdate=this.txtTime.Text;
news.ncontent=this.txtContent.Text;
if(DB.insert(news))
{
Response.Write("插入成功!");
}
else
{
Response.Write("插入失败!");
}
}
} private void Button2_Click(object sender, System.EventArgs e)
{
if(!this.CustomValidator1.IsValid)
{
News news=new News();
news.nid=this.txtType.Text;
news.nyid=this.txtNum.Text;
news.ntitle=this.txtTitle.Text;
news.nnewdate=this.txtTime.Text;
news.ncontent=this.txtContent.Text;
if(DB.update(news))
{
Response.Write("修改成功!");
}
else
{
Response.Write("修改失败!");
}
}
}哪位大哥帮我解决下啊,希望我上面的描述能让您看懂

解决方案 »

  1.   

    我把处理上面那些调用的类也贴出来,好让大哥们看下
    using System;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data;
    namespace WebTest
    {
    /// <summary>
    /// DB 的摘要说明。
    /// </summary>
    /// 
    public class News
    {
    public string nid;
    public string nyid;
    public string ntitle;
    public string nnewdate;
    public string ncontent;
    }
    public class DB
    {
    public DB()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public static SqlConnection createCon()
    {
    string constr=ConfigurationSettings.AppSettings["constr"].ToString();
    SqlConnection con=new SqlConnection(constr);
    return con; }
    public static bool inspect(string sid)
    {
    SqlConnection con=DB.createCon();
    con.Open();
    SqlCommand scd=new SqlCommand("select count(*) from yulenews where yid="+sid,con);
    int count=Convert.ToInt32(scd.ExecuteScalar());
    if(count>0)
    {
    return true;
    }
    else
    {
    return false;
    }
    con.Close();
    }
    public static bool insert(News n)
    {
    try
    {
    SqlConnection con=DB.createCon();
    con.Open();
    SqlCommand scd=new SqlCommand("insert into yulenews values(@nid,@nyid,@ntitle,@nnewdate,@ncontent)",con);
    SqlParameter para=new SqlParameter("@nid",SqlDbType.Int,600);
    para.Value=n.nid;
    scd.Parameters.Add(para);
    para=new SqlParameter("@nyid",SqlDbType.Int,600);
    para.Value=n.nyid;
    scd.Parameters.Add(para);
    para=new SqlParameter("@ntitle",SqlDbType.VarChar,100);
    para.Value=n.ntitle;
    scd.Parameters.Add(para);
    para=new SqlParameter("@nnewdate",SqlDbType.VarChar,50);
    para.Value=n.nnewdate;
    scd.Parameters.Add(para);
    para=new SqlParameter("@ncontent",SqlDbType.VarChar,6000);
    para.Value=n.ncontent;
    scd.Parameters.Add(para);
    scd.ExecuteNonQuery();
    con.Close();
    return true;
    }
    catch(Exception e)
    {
    return false;
    }
    }
    public static bool update(News n)
    {
    try
    {
    SqlConnection con=DB.createCon();
    con.Open();
    SqlCommand scd=new SqlCommand("update yulenews set id="+n.nid+",titil='"+n.ntitle+"',newdate='"+n.nnewdate+"',content='"+n.ncontent+"' where yid="+n.nyid,con);
    scd.ExecuteNonQuery();
    con.Close();
    return true;
    }
    catch(Exception e)
    {
    return false;
    }
    }