前台代码为:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" EnableModelValidation="True"
HorizontalAlign="Center" onrowcommand="GridView1_RowCommand"
onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Eval("id") %>
<%#Eval("bie") %>
<%#Eval("name") %>
<asp:Button ID="Button1" runat="server" Text="编辑" CommandName="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"id") %>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#bind("name") %>'></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="更新" CommandName="Updata" />
<asp:Button ID="Button2" runat="server" Text="取消" CommandName="Canel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码为:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string str = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ceshiConnectionString2"].ConnectionString;
OleDbConnection con = new OleDbConnection(str);
OleDbDataAdapter adp = new OleDbDataAdapter("select * from [chan]", con);
System.Data.DataSet set = new System.Data.DataSet();
adp.Fill(set, "first");
GridView1.DataSource = set.Tables["first"];
DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//if (e.CommandName == "Edit")
//{
GridView1.EditIndex = 3;
//}
}
我有两个疑问:首先为什么当去掉 if (!IsPostBack)时单击编辑按钮gridview不进入编辑模板?其次是当加上 if (!IsPostBack)时为什么第一次单击编辑按钮时不进入编辑模板,只有当单击第二次时才进入?
1.前台
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="用户姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="nPlace" HeaderText="籍贯" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
1.后台
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();//不写到IsPostBack里,每次按钮回发都会执行!
//把绑定代码写到一个函数里,方便重复利用。
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();//编辑完成后要执行绑定!
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from tb_Member where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update tb_Member set name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',sex='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',nPlace='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
public void bind()
{
string sqlstr = "select * from tb_Member";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "tb_Member");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
sqlcon.Close();
}
http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx