asp.net 2.0 GridView RowCommand GridView RowUpdating 事件丢失
没分了,只有这样一点点了。我在asp.net 2.0 中用了一个gridview但是发现RowCommand,RowUpdating事件都不能用了。我其它的事件都正常,我查了代码也没任何问题。是不是事件丢失啊?
.aspx的代码是这样调用的
OnRowUpdating="GridView1_RowUpdating"
.cs的代码是这样的
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
实在查不出错来了,如果是事件丢失我应怎么样解决?
--------------------编程问答-------------------- 是事件不走吧 把初始化绑定的数据
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
}
} --------------------编程问答-------------------- 不是的啊,是有的事件能运行,有的不能运行 ,比如说gridview的GridView1_RowCommand事件就不能运行,捕捉不到,但GridView1_RowEditing又能运行。GridView1_RowCancelingEdit事件也能运行,但GridView1_RowUpdating又不能运行。晕啊 --------------------编程问答-------------------- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
我将.aspx页面中写出来
.cs
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
这个可以执行
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ 这个也行
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
这个也可以
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{这个不行
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{这个也不行
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{这个可以
} --------------------编程问答-------------------- 你把这页所有代码贴出来看看 前后台 --------------------编程问答-------------------- 前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="advice_class.aspx.cs" Inherits="admin_ht_advice_class" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<FONT face="宋体">
<TABLE width="95%" border="0" align="center" cellPadding="0" cellSpacing="0" id="Table1">
<TR>
<TD colspan="3" align="center" bgColor="#6699cc" style="height: 18px"><FONT color="#ffffff">留言分类</FONT></TD>
</TR>
<TR>
<TD colspan="3" align="center" ><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="id" HeaderText="系统ID值" ReadOnly="True" />
<asp:BoundField DataField="ad_name" HeaderText="留言类别" />
<asp:BoundField DataField="orderid" HeaderText="类别排序" />
<asp:CommandField ButtonType="Image" CancelImageUrl="images/cancle.gif" EditImageUrl="images/edit.gif"
HeaderText="编辑" ShowEditButton="True" UpdateImageUrl="images/ok.gif" />
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id")%>'
CommandName="Delete1" ImageUrl="images/delete.gif" OnClientClick="return confirm('确认要删除此项?')"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
还没有任何栏目请先添加栏目
</EmptyDataTemplate>
</asp:GridView>
</TD>
</TR>
<TR>
<TD align="center">分类名称
<asp:TextBox ID="TextBox1" runat="server" Columns="10" MaxLength="255"></asp:TextBox></TD>
<TD>分类排序<asp:TextBox id="TextBox2" runat="server" Columns="4" MaxLength="4"></asp:TextBox></TD>
<TD><asp:Button ID="Button1" runat="server" Text="增加" onclick="Button1_Click"></asp:Button></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</html> --------------------编程问答-------------------- using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class admin_ht_advice_class : System.Web.UI.Page
{
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
ss();
if (!IsPostBack)
{
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (datalayer.ChkPurview("40",Session["UserID"].ToString())==false)
{
Response.Write (client.MessageNoPurview());
Response.End();
return;
}
if (! function.IsNumeric(TextBox2.Text))
{
client.MessageBox(this,"排序不合法", true);
return;
}
if (TextBox1.Text == "")
{
client.MessageBox(this,"类别不能为空", true);
return;
}
DataRow r;
string sql;
sql = "select top 1 * from advice_class";
ds = datalayer.CreateDataSet(sql, "advice_class");
r = ds.Tables["advice_class"].NewRow();
r.BeginEdit();
r["ad_name"] = TextBox1.Text;
r["orderid"] = TextBox2.Text;
r.EndEdit();
ds.Tables["advice_class"].Rows.Add(r);
datalayer.insert_dataset("select * from advice_class", ds, "advice_class");
client.MsgBoxJump(this,"增加成功","advice_class.aspx");
}
private void ss()
{
string sql;
sql = "select * from advice_class order by orderid";
ds = datalayer.CreateDataSet(sql, "advice_class");
GridView1.DataSource = ds.Tables["advice_class"];
ds.Tables["advice_class"].PrimaryKey = new DataColumn[] { ds.Tables["advice_class"].Columns["ID"] };
ViewState["dstemp"] = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete1")
{
if (datalayer.ChkPurview("42",Session["UserID"].ToString())==false)
{
Response.Write (client.MessageNoPurview());
Response.End();
return;
}
string key;
key = Convert.ToInt32(e.CommandArgument).ToString();
if (chkuse(Convert.ToInt32(key)) == true)
{
client.MessageBox(this,"该栏目下面有留言,请先删除留言再删除栏目", true);
return;
}
DataRow r;
ds = (DataSet)ViewState["dstemp"];
r = ds.Tables["advice_class"].Rows.Find(key);
r.Delete();
datalayer.del_dataset("select * from advice_class", ds, "advice_class");
Response.Redirect("advice_class.aspx");
ss();
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
if (datalayer.ChkPurview("41", Session["UserID"].ToString()) == false)
{
Response.Write(client.MessageNoPurview());
Response.End();
return;
}
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState.ToString() == "Alternate, Edit" || e.Row.RowState.ToString() == "Edit")
{
TextBox dp = (TextBox)e.Row.Controls[1].Controls[0];
dp.MaxLength = 50;
dp.Width = 450;
TextBox dp1 = (TextBox)e.Row.Controls[2].Controls[0];
dp1.MaxLength = 5;
dp1.Width = 40;
}
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
public bool chkuse(int ii)
{
bool returnValue;
string sql;
sql = "select * from advice where advice_adclass=" + ii;
System.Data.OleDb.OleDbCommand comm1 = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbConnection conn1 = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbDataReader dr;
conn1.ConnectionString = datalayer.connstr;
conn1.Open();
comm1.Connection = conn1;
comm1.CommandText = sql;
dr = comm1.ExecuteReader();
if (!dr.Read())
{
dr.Close();
conn1.Close();
returnValue = false;
return returnValue;
}
else
{
returnValue = true;
dr.Close();
conn1.Close();
return returnValue;
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = GridView1.EditIndex;
GridViewRow gvr = GridView1.Rows[index];
TextBox dp = (TextBox)gvr.Controls[1].Controls[0];
TextBox dp1 = (TextBox)gvr.Controls[2].Controls[0];
if (dp.Text == "")
{
client.MessageBox(this, "栏目不能为空", true);
e.Cancel = true;
return;
}
if (dp1.Text == "")
{
client.MessageBox(this, "排序不能为空", true);
e.Cancel = true;
return;
}
if (function.IsNumeric(dp1.Text) == false)
{
client.MessageBox(this, "请输入正确的排序", true);
e.Cancel = true;
return;
}
string key;
key = GridView1.DataKeys[index].Value.ToString();
string sql;
sql = "select * from advice_class where id=" + key;
DataSet ds = new DataSet();
ds = datalayer.CreateDataSet(sql, "nn");
DataRow r;
r = ds.Tables["nn"].Rows[0];
r.BeginEdit();
r["ad_name"] = dp.Text.ToString();
r["orderid"] = Convert.ToInt32(dp1.Text.ToString());
r.EndEdit();
datalayer.update_dataset("select * from advice_class", ds, "nn");
GridView1.EditIndex = -1;
ss();
GridView1.DataBind();
}
}
--------------------编程问答-------------------- 数据库操作,查询我是调用没在放上去
--------------------编程问答-------------------- 就是我说的原因 这样就行了 但是不明白你为什么帮了2次???
if (!IsPostBack)
{
ss();
GridView1.DataBind();
} --------------------编程问答-------------------- 搂主怎么判断没执行? --------------------编程问答-------------------- 按你所说的也不行。加了GridView1.DataBind();也是不行的
还有,我判断有没有执行是下断点的
结果跟本没有中断过,所以是没执行的。
--------------------编程问答-------------------- 我同样一模一样的调用,只是数据库字段不一样,文件名不一样就能正常,但这个就不行
昨天还发生一事,就是有个按钮的事件也不执行。但我重新双击按钮,将原来的代码复制到里面,结果就正常了。我发现按钮生成的事件,同我原来手工写的事件一模一样啊。就是搞不明白为什么不执行非要我双击出来的才行。 --------------------编程问答-------------------- 好使了吧 那就行了 呵呵 --------------------编程问答-------------------- 没有啊,我说的是还是不行啊 --------------------编程问答-------------------- 我的意思是,同样的一些代码,另一个文件就正常,这个就不正常。还有昨天也有按钮事件丢失。 --------------------编程问答-------------------- <asp:CommandField ButtonType= "Image " CancelImageUrl= "images/cancle.gif " EditImageUrl= "images/edit.gif "
HeaderText= "编辑 " ShowEditButton= "True " UpdateImageUrl= "images/ok.gif " />
在上述字段中加一个属性: CausesValidation="false"
--------------------编程问答-------------------- 楼上正解!!!
补充:.NET技术 , ASP.NET