首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Gridview的rowupdating事件无法触发。该怎么处理

2012-05-05 
Gridview的rowupdating事件无法触发。前台代码:asp:GridView IDGVrota runatserver AllowSortingT

Gridview的rowupdating事件无法触发。
前台代码:
<asp:GridView ID="GVrota" runat="server" AllowSorting="True" AutoGenerateColumns="False" GridLines="None" DataKeyNames="id0" CellPadding="5" OnRowCancelingEdit="GVrota_RowCancelingEdit" OnRowEditing="GVrota_RowEditing" OnRowUpdating="GVrota_RowUpdating">
  <EmptyDataTemplate>
  数据库连接故障<br />
  </EmptyDataTemplate>
  <Columns>
  <asp:CommandField ShowEditButton="True" />
  <asp:TemplateField HeaderText="日期">
  <HeaderStyle HorizontalAlign="Left" />
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("日期") %>' Width="80px"></asp:Label>
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Left" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="星期">
  <HeaderStyle HorizontalAlign="Center" />
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text='<%# Bind("星期") %>' Width="20px"></asp:Label>
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="公司领导">
  <HeaderStyle HorizontalAlign="Center" />
  <ItemTemplate>
  <asp:Label ID="Label3" runat="server" Text='<%# Bind("公司领导") %>' Width="60px"></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("公司领导") %>'></asp:TextBox>
  </EditItemTemplate>
  <ItemStyle HorizontalAlign="Center" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="值班人员">
  <HeaderStyle HorizontalAlign="Center" />
  <ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Bind("值班人员") %>' Width="60px"></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("值班人员") %>'></asp:TextBox>
  </EditItemTemplate>
  <ItemStyle HorizontalAlign="Center" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="备注">
  <HeaderStyle HorizontalAlign="Left" />
  <ItemTemplate>
  <asp:Label ID="Label5" runat="server" Text='<%# Bind("备注") %>' Width="100px"></asp:Label>


  </ItemTemplate>
  <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("备注") %>'></asp:TextBox>
  </EditItemTemplate>
  <ItemStyle HorizontalAlign="Left" />
  </asp:TemplateField>
  <asp:BoundField DataField="id0" HeaderText="id0" Visible="False" />
  </Columns>
  </asp:GridView>
后台代码:
private void initData()
  {
  DateTime dt = DateTime.Now;
  string queryDT = dt.Year.ToString() + "-" + dt.Month.ToString();
  lTitle.Text = dt.Year.ToString() + "年" + dt.Month.ToString() + "月份公司总值班表";
  string cmdText = "SELECT [id0] ,[日期] ,[星期] ,[公司领导] ,[值班人员], [备注] FROM 总值班表 WHERE [日期] LIKE @DT ORDER BY [日期]";
  SqlDataReader ds = SqlHelper.ExecuteReader(CommandType.Text, cmdText, new SqlParameter("@DT", "%" + queryDT + "%"));
  GVrota.DataSource = ds;
  GVrota.DataBind();
  }  

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
  //
  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  //
  InitializeComponent();
  base.OnInit(e);
  }

  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {

  }
  #endregion


  protected void GVrota_RowEditing(object sender, GridViewEditEventArgs e)
  {
  GVrota.EditIndex = e.NewEditIndex;
  initData();
  }
  protected void GVrota_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
  GVrota.EditIndex = -1;
  initData();
  }
  protected void GVrota_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
  string editID = GVrota.DataKeys[e.RowIndex].Value.ToString();
  string cmdText1 = "UPDATE [总值班表] SET [公司领导]=@leader ,[值班人员]=@manager ,[备注]=@memo WHERE [id0]=@id";
  string leader = ((TextBox)GVrota.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
  string manager = ((TextBox)GVrota.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
  string memo = ((TextBox)GVrota.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
  SqlHelper.ExecuteScalar(CommandType.Text, cmdText1, new SqlParameter("@leader", leader), new SqlParameter("@manager", manager), new SqlParameter("@memo", memo), new SqlParameter("@id", editID));
  GVrota.EditIndex = -1;
  initData();
  }
编辑和取消按钮的事件都没问题,就是更新点了一闪就恢复原样,没啥反应,设了断点发现rowupdating根本就没有执行。不知道为什么,求助!!!!!!
看到别人说是web.config里面enableviewstate=false导致,我根本就没这一条,不知道还有什么原因????

[解决办法]
在命令中设置CauseesValidation 为false看看
[解决办法]
if (!IsPostBack)
{
内容····
}
不写这个修改的时候就是返回原来的样子了···
例子如下所示:
namespace grikongjian
{
public partial class _Default : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fangfa();
}
}

public void fangfa()
{
string strcon = "select * from biao";
DataSet ds = SqlHelper.gda(strcon);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}

//实现更新和取消
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
fangfa();
}

//修改
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string uid = this.GridView1.Rows[e.RowIndex].Cells[0].Text;
TextBox uname = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox sex = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox age = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox imger = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
string strcon = "update biao set uname='" + uname.Text + "',sex='" + sex.Text + "',age='" + age.Text + "',imger='" + imger.Text + "' where uid="+uid;

SqlHelper.Exc(strcon);
this.GridView1.EditIndex = -1;
fangfa();
}

//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string uid = this.GridView1.Rows[e.RowIndex].Cells[0].Text;
string strcon = "delete from biao where uid=" + uid;
SqlHelper.Exc(strcon);
fangfa();
}


//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
fangfa();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
{
LinkButton lb = (LinkButton)e.Row.Cells[5].Controls[2];
if (lb.Text == "删除")
{
lb.Attributes.Add("onclick", "shan();");
}
}
}
}
}

热点排行