[英]How to differentiate updating or inserting in GridView OnUpdating Event?
[英]How to create gridview updating event dynamically?
public partial class Gridvw_expt2 : System.Web.UI.Page
{
SqlCommand com;
SqlDataAdapter da;
DataSet ds;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["gj"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
com = new SqlCommand("Select * from tblExpt",con);
da = new SqlDataAdapter(com);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows[0] != null)
{
GridView1.AutoGenerateEditButton = true;
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.RowUpdating += new GridViewUpdateEventHandler(abc);
GridView1.DataKeyNames = new string[] { "id" };
GridView1.RowEditing += new GridViewEditEventHandler(bc);
}
else
Response.Write("fkj");
}
protected void abc(object sender, GridViewUpdateEventArgs e)
{
Response.Write(e.RowIndex);
}
protected void bc(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)sender;
gv.EditIndex = e.NewEditIndex;
}
}
仅当我编辑下一行时,该行才进入编辑模式,这意味着第一行从不进入编辑模式。请帮助为什么。
代替
GridView1.Attributes.Add("onrowupdating", "abc");
做这个:
GridView1.RowUpdating += new GridViewUpdateEventHandler(abc);
另外,代替
GridView1.Attributes.Add("DataKeyNames", "id");
做这个
GridView1.DataKeyNames = new string[] { "id" };
也是x 2,而不是
if (ds.Tables[0].Rows[0].ToString() != null)
做这个
if (ds.Tables[0].Rows[0] != null) //.ToString() will cause an exception if it is actuall null
为什么我觉得我在教课:)
这是因为您尚未设置处理程序来处理GridView.RowEditing 。 在您的gridview中(在.aspx中),您需要连接一个将处理RowEditing的方法。
您的gridview代码如下所示:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
您需要添加:
OnRowEditing="nameOfMethodYouWantToFire"
所以看起来像:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="nameOfMethodYouWantToFire">
</asp:GridView>
nameOfMethodYouWantToFire
在您的代码后面(您的C#)中,它可以处理事件。 像这样:
protected void nameOfMethodYouWantToFire(object sender, GridViewPageEventArgs e)
{
//Your code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.