哪位高手帮我看看,DataGrid1的事件为什么不执行。
代码如下:
protected Infragistics.WebUI.UltraWebTab.UltraWebTab UltraWebTab1;
protected static string id = null;
protected static string id2 = null;private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
ReadAndPrintClass1();
ReadAndPrintClass2();
}
}private void ReadAndPrintClass1()
{
Sql sqlserve = new Sql();
DataSet ds = sqlserve.ReadData("Class1List");
DataGrid dg = (DataGrid)UltraWebTab1.FindControl("DataGrid1");
dg.DataSource = ds.Tables[0];
dg.DataBind();
}

private void ReadAndPrintClass2()
{
Sql sqlserve = new Sql();
DataSet ds2 = sqlserve.ReadData("Class2List");
DataGrid dg2 = (DataGrid)UltraWebTab1.FindControl("DataGrid2");
dg2.DataSource = ds2.Tables[0];
dg2.DataBind();
}override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//

DataGrid dg2 = (DataGrid)UltraWebTab1.FindControl("DataGrid2");
dg2.DeleteCommand += new DataGridCommandEventHandler(dg2_DeleteCommand);
dg2.EditCommand += new DataGridCommandEventHandler(dg2_EditCommand); DataGrid dg = (DataGrid)UltraWebTab1.FindControl("DataGrid1");
dg.DeleteCommand += new DataGridCommandEventHandler(dg_DeleteCommand);
dg.EditCommand += new DataGridCommandEventHandler(dg_EditCommand);

InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load);
}
#endregionprivate void dg_EditCommand(object source, DataGridCommandEventArgs e)
{
Panel pl = (Panel)UltraWebTab1.FindControl("Panel1");
pl.Visible = true;
id = e.Item.Cells[0].Text;
TextBox tb1 = (TextBox)UltraWebTab1.FindControl("TextBox1");
tb1.Text = id;
TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
tb2.Text = e.Item.Cells[1].Text;
ReadAndPrintClass1();}private void dg2_EditCommand(object source, DataGridCommandEventArgs e)
{
Panel pl2 = (Panel)UltraWebTab1.FindControl("Panel2");
pl2.Visible = true;
id2 = e.Item.Cells[0].Text;
TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
tb3.Text = id2;
TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
tb4.Text = e.Item.Cells[1].Text;
ListClass1();
DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
ddl.SelectedValue = e.Item.Cells[3].Text;
}private void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
Sql sqlserve = new Sql();
if (sqlserve.ChangeData("DelClass1Single",e.Item.Cells[0].Text))
{
Label lb = (Label)UltraWebTab1.FindControl("Label3");
lb.Text = "删除数据成功";
}
else
{
Label lb = (Label)UltraWebTab1.FindControl("Label3");
lb.Text = "删除数据失败";
}
id = "";
ReadAndPrintClass1();
}private void dg2_DeleteCommand(object source, DataGridCommandEventArgs e)
{
Sql sqlserve = new Sql();
if (sqlserve.ChangeData("DelClass2Single",e.Item.Cells[0].Text))
{
Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
lb7.Text = "成功删除数据";
}
else
{
Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
lb7.Text = "删除数据失败";
}
ReadAndPrintClass2();
}private void ListClass1()
{
Sql sqlserve = new Sql();
DataSet ds = sqlserve.ReadData("Class1List");
DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
ddl.DataSource = ds.Tables[0];
ddl.DataTextField = "cla1_name";
ddl.DataValueField = "cla1_id";
ddl.DataBind();
}

解决方案 »

  1.   

    我没有这样使用过datagrid,不过根据你给的代码,我觉得大部分都是正确的除了没有看到你在事件按钮比如linkbutton上的commandName属性中不知道你是否把它们的参数正确的设置了Edit,Delete,Cancel的属性,只有把button控件的commandName属性正确的做了设置以后它才能正确的调用相关的函数,
      

  2.   

    跟踪一下在事件触发的时候有没有执行,
    只要你将控件的事件绑定到相关的函数就能执行的呀
    这个应该是ASP.NET 的事件触发模型
      

  3.   

    还发现的一个问题是你的Edititemindex的属性并没有做任何的设置,这样你的编辑项怎么确定看起来问题还是比较的多
      

  4.   

    先谢谢各位。补充几点:
    1.数据绑定正常,DataGrid1和DataGrid2都能正常显示数据,DataGrid2的所有事件都正常触发。
    2.将DataGrid2全部删除,DataGrid1就全部正常,这样看来事件绑定也应该是正常的。
    3.调试的时候我把一些代码注释掉了,那些代码我就没有贴上来,下面我把全部代码贴上。
    ===========================================
    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 WZWS
    {
    /// <summary>
    /// Admin_Class 的摘要说明。
    /// </summary>
    public class Admin_Class : System.Web.UI.Page
    {
    //protected System.Web.UI.HtmlControls.HtmlGenericControl P1;
    protected Infragistics.WebUI.UltraWebTab.UltraWebTab UltraWebTab1;
    protected static string id = null;
    protected static string id2 = null;
    //protected static string className = null;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if (!IsPostBack)
    {
    ReadAndPrintClass1();
    ReadAndPrintClass2();
    }
    } private void ReadAndPrintClass1()
    {
    Sql sqlserve = new Sql();
    DataSet ds = sqlserve.ReadData("Class1List");
    DataGrid dg = (DataGrid)UltraWebTab1.FindControl("DataGrid1");
    dg.DataSource = ds.Tables[0];
    dg.DataBind();
    }

    private void ReadAndPrintClass2()
    {
    Sql sqlserve = new Sql();
    DataSet ds2 = sqlserve.ReadData("Class2List");
    DataGrid dg2 = (DataGrid)UltraWebTab1.FindControl("DataGrid2");
    dg2.DataSource = ds2.Tables[0];
    dg2.DataBind();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //

    DataGrid dg2 = (DataGrid)UltraWebTab1.FindControl("DataGrid2");
    dg2.DeleteCommand += new DataGridCommandEventHandler(dg2_DeleteCommand);
    dg2.EditCommand += new DataGridCommandEventHandler(dg2_EditCommand); DataGrid dg = (DataGrid)UltraWebTab1.FindControl("DataGrid1");
    dg.DeleteCommand += new DataGridCommandEventHandler(dg_DeleteCommand);
    dg.EditCommand += new DataGridCommandEventHandler(dg_EditCommand);
    /*
    Button bt = (Button)UltraWebTab1.FindControl("Button1");
    bt.Click += new EventHandler(bt_Click);
    Button bt2 = (Button)UltraWebTab1.FindControl("Button2");
    bt2.Click += new EventHandler(bt2_Click);
    Button bt3 = (Button)UltraWebTab1.FindControl("Button3");
    bt3.Click += new EventHandler(bt3_Click);

    Button bt4 = (Button)UltraWebTab1.FindControl("Button4");
    bt4.Click += new EventHandler(bt4_Click);
    Button bt5 = (Button)UltraWebTab1.FindControl("Button5");
    bt5.Click += new EventHandler(bt5_Click);
    Button bt6 = (Button)UltraWebTab1.FindControl("Button6");
    bt6.Click += new EventHandler(bt6_Click);
    */
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion private void bt_Click(object sender, EventArgs e)
    {
    Panel pl = (Panel)UltraWebTab1.FindControl("Panel1");
    pl.Visible = true;
    id = "";
    className = "";
    TextBox tb1 = (TextBox)UltraWebTab1.FindControl("TextBox1");
    tb1.Text = "(自动生成)";
    TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
    tb2.Text = "";
    ReadAndPrintClass1();
    } private void bt2_Click(object sender, EventArgs e)
    {
    CheckFormat check = new CheckFormat();
    if (check.CheckIsNull(id))
    {
    TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
    string sqlText = "Insert Into class1(cla1_name) Values('"+tb2.Text+"')";
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeDataDirect(sqlText))
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "添加数据成功";
    }
    else
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "添加数据失败";
    }
    }
    else
    {
    TextBox tb1 = (TextBox)UltraWebTab1.FindControl("TextBox1");
    TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
    string sqlText = "UpDate Class1 set cla1_name ='"+tb2.Text+"' where cla1_id = "+tb1.Text;
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeDataDirect(sqlText))
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "修改数据成功";
    }
    else
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "修改数据失败";
    }
    }
    Panel pl = (Panel)UltraWebTab1.FindControl("Panel1");
    pl.Visible = false;
    ReadAndPrintClass1();
    } private void bt3_Click(object sender, EventArgs e)
    {
    Panel pl = (Panel)UltraWebTab1.FindControl("Panel1");
    pl.Visible = true;
    TextBox tb1 = (TextBox)UltraWebTab1.FindControl("TextBox1");
    tb1.Text = id;
    TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
    tb2.Text = className;
    } private void dg_EditCommand(object source, DataGridCommandEventArgs e)
    {
    Panel pl = (Panel)UltraWebTab1.FindControl("Panel1");
    pl.Visible = true;
    id = e.Item.Cells[0].Text;
    TextBox tb1 = (TextBox)UltraWebTab1.FindControl("TextBox1");
    tb1.Text = id;
    TextBox tb2 = (TextBox)UltraWebTab1.FindControl("TextBox2");
    //className = 
    tb2.Text = e.Item.Cells[1].Text;
    ReadAndPrintClass1(); } private void dg2_EditCommand(object source, DataGridCommandEventArgs e)
    {
    Panel pl2 = (Panel)UltraWebTab1.FindControl("Panel2");
    pl2.Visible = true;
    id2 = e.Item.Cells[0].Text;
    TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
    tb3.Text = id2;
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    //className = 
    tb4.Text = e.Item.Cells[1].Text;
    ListClass1();
    DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    //Response.Write("<script>alert('"+e.Item.Cells[1].ToString()+"');</script>");
    ddl.SelectedValue = e.Item.Cells[3].Text;
    } private void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeData("DelClass1Single",e.Item.Cells[0].Text))
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "删除数据成功";
    }
    else
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label3");
    lb.Text = "删除数据失败";
    }
    id = "";
    //className = "";
    ReadAndPrintClass1();
    }
      

  5.   

    private void dg2_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeData("DelClass2Single",e.Item.Cells[0].Text))
    {
    Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
    lb7.Text = "成功删除数据";
    }
    else
    {
    Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
    lb7.Text = "删除数据失败";
    }
    ReadAndPrintClass2();
    } private void bt4_Click(object sender, EventArgs e)
    {
    Panel pl2 = (Panel)UltraWebTab1.FindControl("Panel2");
    pl2.Visible = true;
    id2 = "";
    TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
    tb3.Text = "(自动生成)";
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    tb4.Text = "";
    ListClass1();
    } private void bt5_Click(object sender, EventArgs e)
    {
    //DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    //Response.Write("<script>alert('"+ddl.SelectedValue+"');</script>");
    //ddl.SelectedValue = e.Item.Cells[1].ToString();

    CheckFormat check = new CheckFormat();
    if (check.CheckIsNull(id2))
    {
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    string sqlText = "Insert Into class2(cla2_name,cla2_cla1_id) Values('"+tb4.Text+"',"+ddl.SelectedValue+")";
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeDataDirect(sqlText))
    {
    Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
    lb7.Text = "添加数据成功";
    }
    else
    {
    Label lb7 = (Label)UltraWebTab1.FindControl("Label7");
    lb7.Text = "添加数据失败";
    }
    }
    else
    {
    TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    string sqlText = "UpDate Class2 set cla2_name ='"+tb4.Text+"',cla2_cla1_id = "+ddl.SelectedValue+" where cla2_id = "+tb3.Text;
    Sql sqlserve = new Sql();
    if (sqlserve.ChangeDataDirect(sqlText))
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label7");
    lb.Text = "修改数据成功";
    }
    else
    {
    Label lb = (Label)UltraWebTab1.FindControl("Label7");
    lb.Text = "修改数据失败";
    }
    }
    Panel pl2 = (Panel)UltraWebTab1.FindControl("Panel2");
    pl2.Visible = false;
    ReadAndPrintClass2();
    } private void bt6_Click(object sender, EventArgs e)
    {
    CheckFormat check = new CheckFormat();
    if (check.CheckIsNull(id2))
    {
    Panel pl2 = (Panel)UltraWebTab1.FindControl("Panel2");
    pl2.Visible = true;
    id2 = "";
    TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
    tb3.Text = "(自动生成)";
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    tb4.Text = "";
    ListClass1();
    }
    else
    {
    DataGrid dg = (DataGrid)UltraWebTab1.FindControl("DataGrid2");
    foreach (DataGridItem row in dg.Items)
    {
    if (row.Cells[0].Text == id2)
    {
    TextBox tb3 = (TextBox)UltraWebTab1.FindControl("TextBox3");
    TextBox tb4 = (TextBox)UltraWebTab1.FindControl("TextBox4");
    DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    tb3.Text = row.Cells[0].Text;
    tb4.Text = row.Cells[1].Text;
    ddl.SelectedValue = row.Cells[3].Text;
    }
    }
    }
    } private void ListClass1()
    {
    Sql sqlserve = new Sql();
    DataSet ds = sqlserve.ReadData("Class1List");
    DropDownList ddl = (DropDownList)UltraWebTab1.FindControl("DropDownList1");
    ddl.DataSource = ds.Tables[0];
    ddl.DataTextField = "cla1_name";
    ddl.DataValueField = "cla1_id";
    ddl.DataBind();
    } }
    }