是不是codebehind? 把page_Load里的代码也贴出来

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    string names;string password;
    HttpCookie Cookie;
    Cookie=new HttpCookie("mychoices");
    try 
    {
    if (Request.Cookies["mychoices"].Values["logname"]!="") 
    {
    if(Request.Cookies["mychoices"].Values["pass"]!="")  
    {  

    names =Request.Cookies["mychoices"].Values["logname"];
    password=Request.Cookies["mychoices"].Values["pass"]; 
    }
    }
    }
    catch  
    {

    Response.Redirect("error.aspx");

    }
    {
    if(!Page.IsPostBack)
    {

    SqlConnection Con = new SqlConnection("Data Source=localhost;Initial Catalog=xuanke;User ID=sa;Pwd=;Integrated Security=true;");  
    DataSet ds;
    SqlDataAdapter Cmd;
    string sel="select * from kecheng ";
    Cmd=new SqlDataAdapter(sel,Con);
    ds=new DataSet();
    Cmd.Fill(ds,"kecheng");
    kc.DataSource=ds.Tables["kecheng"].DefaultView;
                            
    kc.DataBind();
        
    }
    }
    }
      

  2.   

    binddata()这段代码也放在page_Load中的
      

  3.   

    代码似乎没错,检查AutoEventWireup是否为"false"
      

  4.   

    检查代码中kc_Delete事件是否被多次注册
      

  5.   

    第二次的id,是第一次id后面紧跟着的一个?
      

  6.   

    属性里没你说的AutoEventWireup啊?在哪儿呢?
      

  7.   

    建议还是用DataGrid默认的删除方法名DataGrid1_DeleteCommand
      

  8.   

    右键设计器里的datagrid,属性,有个闪电图标的,是事件,在那里有个DeleteCommand,双击即可
      

  9.   


    http://dotnet.aspx.cc/ShowDetail.aspx?id=8ADE535F-AD40-4DE3-A962-A64B4FAF12C4
      

  10.   

    我感觉没区别啊?我把所有代码复制下来,大家看看:
    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 xuanke
    {
    /// <summary>
    /// cedit 的摘要说明。
    /// </summary>
    public class cedit : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.DataGrid kc;
    protected System.Web.UI.WebControls.TextBox tiaojian;
    protected System.Web.UI.WebControls.Button retrieve;

    private void Page_Load(object sender, System.EventArgs e)
    {
    string names;string password;
    HttpCookie Cookie;
    Cookie=new HttpCookie("mychoices");
    try 
    {
    if (Request.Cookies["mychoices"].Values["logname"]!="") 
    {
    if(Request.Cookies["mychoices"].Values["pass"]!="")  
    {  

    names =Request.Cookies["mychoices"].Values["logname"];
    password=Request.Cookies["mychoices"].Values["pass"]; 
    }
    }
    }
    catch  
    {

    Response.Redirect("error.aspx");

    }
    {
    if(!Page.IsPostBack)
    {

    SqlConnection Con = new SqlConnection("Data Source=localhost;Initial Catalog=xuanke;User ID=sa;Pwd=;Integrated Security=true;");  
    DataSet ds;
    SqlDataAdapter Cmd;
    string sel="select * from kecheng ";
    Cmd=new SqlDataAdapter(sel,Con);
    ds=new DataSet();
    Cmd.Fill(ds,"kecheng");
    kc.DataSource=ds.Tables["kecheng"].DefaultView;
                            
    kc.DataBind();
        
    }
    }
    } public void binddata()
    {

    SqlConnection Con = new SqlConnection("Data Source=localhost;Initial Catalog=xuanke;User ID=sa;Pwd=;Integrated Security=true;");  
        DataSet ds;
    Con.Open();
    SqlDataAdapter Cmd;
    string sel="select * from kecheng where " +DropDownList1.SelectedValue+" like '%"+ tiaojian.Text.ToString()+"%'";
    Cmd=new SqlDataAdapter(sel,Con);
    ds=new DataSet();
    Cmd.Fill(ds,"kecheng");
    kc.DataSource=ds.Tables["kecheng"].DefaultView;
    kc.DataBind();
    Con.Close();
    }

    protected void kc_Edit(Object sender, DataGridCommandEventArgs E) 


    kc.EditItemIndex = (int)E.Item.ItemIndex; 
    binddata(); 
    }
    protected void kc_Cancel(Object sender, DataGridCommandEventArgs E) 
    {

    kc.EditItemIndex = -1; 
    binddata(); 
    }
    protected void kc_Update(Object sender, DataGridCommandEventArgs E)
    {
    string SQL="UPDATE kecheng SET cname=@cname,cengci=@cengci,xuefen=@xuefen,qishizhou=@qishizhou,xueshi=@xueshi,tno=@tno,tname=@tname,tzhicheng=@tzhicheng,duixiang=@duixiang WHERE cid=@cid";
    SqlConnection Con = new SqlConnection("Data Source=localhost;Initial Catalog=xuanke;User ID=sa;Pwd=;Integrated Security=true;");  
    SqlCommand delCommand =new SqlCommand(SQL,Con);
    delCommand.Parameters.Add(new SqlParameter("@cid",SqlDbType.Char,10));
    delCommand.Parameters["@cid"].Value=kc.DataKeys[(int)E.Item.ItemIndex].ToString(); delCommand.Parameters.Add(new SqlParameter("@cname",SqlDbType.Char,50));
    delCommand.Parameters["@cname"].Value=((TextBox)E.Item.Cells[1].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@cengci",SqlDbType.Char,6));
    delCommand.Parameters["@cengci"].Value=((TextBox)E.Item.Cells[2].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@xuefen",SqlDbType.Decimal,9));
                delCommand.Parameters["@xuefen"].Value=decimal.Parse(((TextBox)E.Item.Cells[3].Controls[0]).Text); delCommand.Parameters.Add(new SqlParameter("@qishizhou",SqlDbType.Char,10));
    delCommand.Parameters["@qishizhou"].Value=((TextBox)E.Item.Cells[4].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@xueshi",SqlDbType.Char,2));
    delCommand.Parameters["@xueshi"].Value=((TextBox)E.Item.Cells[5].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@tno",SqlDbType.Char,10));
                delCommand.Parameters["@tno"].Value=((TextBox)E.Item.Cells[6].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@tname",SqlDbType.Char,10));
                delCommand.Parameters["@tname"].Value=((TextBox)E.Item.Cells[7].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@tzhicheng",SqlDbType.Char,10));
    delCommand.Parameters["@tzhicheng"].Value=((TextBox)E.Item.Cells[8].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@duixiang",SqlDbType.Char,20));
    delCommand.Parameters["@duixiang"].Value=((TextBox)E.Item.Cells[9].Controls[0]).Text; delCommand.Parameters.Add(new SqlParameter("@leixing",SqlDbType.Char,20));
    delCommand.Parameters["@leixing"].Value=((TextBox)E.Item.Cells[9].Controls[0]).Text;
    delCommand.Connection.Open();
    delCommand.ExecuteNonQuery();
    delCommand.Connection.Close();
    kc.EditItemIndex = -1;
    binddata();


    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    binddata();
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.retrieve.Click += new System.EventHandler(this.Button1_Click);
    this.kc.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.kc_ItemCreated);
    this.kc.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.kc_Delete);
    this.kc.SelectedIndexChanged += new System.EventHandler(this.kc_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion public void kc_Delete(object source, DataGridCommandEventArgs E)
    {
    SqlConnection Con = new SqlConnection("Data Source=localhost;Initial Catalog=xuanke;User ID=sa;Pwd=;Integrated Security=true;");  
    string sqlstr="DELETE FROM kecheng WHERE cid=@cid";
    SqlCommand delCommandl =new SqlCommand(sqlstr,Con);
    delCommand.Parameters.Add(new SqlParameter("@cid",SqlDbType.Char,10));
    delCommand.Parameters["@cid"].Value=kc.DataKeys[(int)E.Item.ItemIndex].ToString();
    Con.Open();
    delCommand.ExecuteNonQuery();
    Con.Close();
    binddata();
    } private void kc_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } private void kc_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {

    if(e.Item.ItemIndex>=0) 

    Button LB=(Button)e.Item.Cells[((DataGrid)sender).Columns.Count-1].Controls[0];
    LB.Attributes.Add("onclick", "if(!confirm('确定要删除该行数据吗?')) return false;");
    }
    if(e.Item.ItemIndex>=0) 

    Button LB=(Button)e.Item.Cells[((DataGrid)sender).Columns.Count-2].Controls[0];
    LB.Attributes.Add("onclick", "if(!confirm('确定要修改该行数据吗?')) return false;");
    }
    }
    }
    }
      

  11.   

    datagrid中的两行数据,删除哪个语句执行了2次
      

  12.   

    你自己看看,两个这个
    if(e.Item.ItemIndex>=0) 

    Button LB=(Button)e.Item.Cells[((DataGrid)sender).Columns.Count-1].Controls[0];
    LB.Attributes.Add("onclick", "if(!confirm('确定要删除该行数据吗?')) return false;");
    }
    if(e.Item.ItemIndex>=0) 

    Button LB=(Button)e.Item.Cells[((DataGrid)sender).Columns.Count-2].Controls[0];
    LB.Attributes.Add("onclick", "if(!confirm('确定要修改该行数据吗?')) return false;");