是不是没有在Page_Load事件中判断是否是PostBack?如果判断了,最好把源代码粘贴出来。这样才能知道错误在哪.

解决方案 »

  1.   

    你增加的删除按纽,在什么位置?如果不是在Page_load中调用了你增加的删除按纽,那么refresh之后,你的按纽就没了。或者,postback之后,按纽就没了。
      

  2.   

    我的代码如下:我刚学着将代码和页面分开,我只是想显示出DataGrid,并能执行删除操作.
    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;namespace Web1
    {
    /// <summary>
    /// Summary description for WebForm2.
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.Label Label6;
    protected System.Web.UI.WebControls.Label Label7;
    protected System.Web.UI.WebControls.Label Label8;
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.TextBox TextBox4;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.WebControls.DropDownList DropDownList2;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.RangeValidator RV1;
    protected System.Web.UI.WebControls.Label Label9;
    protected System.Web.UI.WebControls.Label Label10;
    protected System.Web.UI.WebControls.Label Label11;
    protected System.Web.UI.WebControls.Label Label12;
    protected System.Web.UI.WebControls.TextBox TextBox5;

    public WebForm2()
    {
    Page.Init += new System.EventHandler(Page_Init);
    } private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if (this.IsPostBack)
    return;
                       showgrid();
    } private void Page_Init(object sender, EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    } #region Web Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void showgrid()
    {
    string strdsn="server=ls;uid=sa;pwd=9876;database=hdb";
    System.Data.SqlClient.SqlDataAdapter objada=new System.Data.SqlClient.SqlDataAdapter();
    System.Data.SqlClient.SqlConnection objconn=new System .Data.SqlClient.SqlConnection(strdsn);

    string strsql="select * from table1";
    objada.TableMappings.Add("Table","table1");
    objada.SelectCommand=new System.Data.SqlClient.SqlCommand(strsql,objconn);
    System.Data.DataSet objds=new System.Data.DataSet ("table1");
    objada.Fill(objds);
    this.DataGrid1.DataSource=objds;
    this.DataGrid1.DataBind();
    } private void DataGrid1_DeleteCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string strno=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
    string strsql="DELETE * FROM table1 WHERE (bh='" +strno+ "')";
    string strdsn="server=ls;uid=sa;pwd=9876;database=hdb";
    System.Data.SqlClient.SqlDataAdapter objada=new System.Data.SqlClient.SqlDataAdapter();
    System.Data.SqlClient.SqlConnection objconn=new System .Data.SqlClient.SqlConnection(strdsn);
    System.Data.SqlClient.SqlCommand objcmd =new System.Data.SqlClient.SqlCommand(strsql,objconn);
    objada.TableMappings.Add("Table","table1");
    objada.SelectCommand=new System.Data.SqlClient.SqlCommand(strsql,objconn);
    System.Data.DataSet objds=new System.Data.DataSet ("table1");
    objada.Fill(objds);
    this.DataGrid1.DataSource=objds;
    this.DataGrid1.DataBind();
    objcmd.Connection.Open();
    objcmd.ExecuteNonQuery();
    objcmd.Connection.Close();
      

  3.   

    public void DataGrid1_DeleteCommand(Object sender,DataGridCommandEventArgs e)
    {
        if (e.CommandName.Trim()=="Delete")
        {
        }}
      

  4.   

    DataGrid1_DeleteCommand的访问修饰符不可以为private
      

  5.   

    按照你的代码,其它的我没有看,我只看了事件响应的地方,你的写法是对的。如果依然还没有反映,那么你看看你的HTML源代码,DataGrid是否放到了命名空间的<Form></Form>之间。如果DataGrid放到了<Form></Form>以外的地方,页面是不会自动提交的。如果的确在<Form></Form>之间,我建议把事件响应函数中的代码去掉,再调试。如果还有错,把错误代码粘贴出来吧。
      

  6.   

    按楼上诸位的方法事件好象是响应了,但事件中:
    string strno=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
    出错,说"牵引超出范围,必须为非负并小于集合大小,参数名:index"
    我设置的主键是bh字段,char,3
    我实在是很闹心,怎么代码和页面分开这么不易吗?有哪位能提供给我一份完整的使用DataGrid 执行删除或编辑的源代码吗?不胜感谢!