前台
<form id="form1" runat="server" >
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" Font-Size="10pt">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table style="width: 100%">
<tr>
<td style="width: 100px">
姓名</td>
<td style="width: 100px">
一级主管评分</td>
<td style="width: 100px">
二级主管评分</td>
<td style="width: 100px">
三级主管评分</td>
<td style="width: 100px">
综合评分</td>
<td style="width: 100px">
编辑</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xm") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs1") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs2") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs3") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs") %></td>
<td style="width: 100px; height: 21px">
<asp:LinkButton ID="edit" runat="server" CommandName="edit" Text="编辑"></asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table style="width: 100%">
<tr>
<td style="width: 100px">
<%# DataBinder.Eval (Container.DataItem,"xm") %></td>
<td style="width: 100px">
<asp:TextBox ID="xs1" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs1") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="xs2" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs2") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="xs3" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs3") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<%# DataBinder.Eval (Container.DataItem,"xs") %></td>
<td style="width: 120px">
<asp:LinkButton ID="update" runat="server" CommandName="update" Text="确认"></asp:LinkButton><asp:LinkButton ID="cancel" runat="server" CommandName="cancel" Text="取消"></asp:LinkButton></td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
后台
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; public partial class xs : System.Web.UI.Page
{
//加载
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
//绑定
public void bind()
{
SqlConnection sqlcon = new SqlConnection("server=192.168.0.231;database=iOffice;uid=sa;pwd=future?20061101");
string sqlstr = "select a.id,b.name as xm,a.xs,a.xs1,a.xs2,a.xs3 from mrArchive c inner join stras a on c.EmpID=a.EmpID inner join mrBaseInf b on a.EmpID=b.EmpID where c.FDepID1=21";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "stras");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection sqlcon = new SqlConnection("server=192.168.0.231;database=iOffice;uid=sa;pwd=future?20061101");
string st = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString();
string sqlstr = "update stras set xs1='" + ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString() + "',xs2='"
+ ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs2")).Text.ToString() + "',xs3='"
+ ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs3")).Text.ToString() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
Response.Write(st);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
}
为什么编辑之后,点更新按钮时。((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString()取到的是绑定值,而不是在编辑时所修改值,即编辑之前的值。
<form id="form1" runat="server" >
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" Font-Size="10pt">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table style="width: 100%">
<tr>
<td style="width: 100px">
姓名</td>
<td style="width: 100px">
一级主管评分</td>
<td style="width: 100px">
二级主管评分</td>
<td style="width: 100px">
三级主管评分</td>
<td style="width: 100px">
综合评分</td>
<td style="width: 100px">
编辑</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xm") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs1") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs2") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs3") %></td>
<td style="width: 100px; height: 21px">
<%# DataBinder.Eval (Container.DataItem,"xs") %></td>
<td style="width: 100px; height: 21px">
<asp:LinkButton ID="edit" runat="server" CommandName="edit" Text="编辑"></asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table style="width: 100%">
<tr>
<td style="width: 100px">
<%# DataBinder.Eval (Container.DataItem,"xm") %></td>
<td style="width: 100px">
<asp:TextBox ID="xs1" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs1") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="xs2" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs2") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="xs3" width="70" Text='<%# DataBinder.Eval (Container.DataItem,"xs3") %>' runat="server" ></asp:TextBox></td>
<td style="width: 100px">
<%# DataBinder.Eval (Container.DataItem,"xs") %></td>
<td style="width: 120px">
<asp:LinkButton ID="update" runat="server" CommandName="update" Text="确认"></asp:LinkButton><asp:LinkButton ID="cancel" runat="server" CommandName="cancel" Text="取消"></asp:LinkButton></td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
后台
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; public partial class xs : System.Web.UI.Page
{
//加载
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
//绑定
public void bind()
{
SqlConnection sqlcon = new SqlConnection("server=192.168.0.231;database=iOffice;uid=sa;pwd=future?20061101");
string sqlstr = "select a.id,b.name as xm,a.xs,a.xs1,a.xs2,a.xs3 from mrArchive c inner join stras a on c.EmpID=a.EmpID inner join mrBaseInf b on a.EmpID=b.EmpID where c.FDepID1=21";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "stras");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };//主键
GridView1.DataBind();
sqlcon.Close();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection sqlcon = new SqlConnection("server=192.168.0.231;database=iOffice;uid=sa;pwd=future?20061101");
string st = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString();
string sqlstr = "update stras set xs1='" + ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString() + "',xs2='"
+ ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs2")).Text.ToString() + "',xs3='"
+ ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs3")).Text.ToString() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
Response.Write(st);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
}
为什么编辑之后,点更新按钮时。((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("xs1")).Text.ToString()取到的是绑定值,而不是在编辑时所修改值,即编辑之前的值。
解决方案 »
- 求程序启动时,自动更新的思路!
- pda 智能设备程序 窗体的事件没有FormClosing,只有closing,但是后者没有反应
- System.Net.WebClient的DownloadData()方法下载某个网页内容报错,请帮忙!!
- 如何知道下划线的线宽
- list的小问题?
- 用C#编程调整罗技摄像头设置○●○●○●○●○●○●○●○●
- 一个关于ExecuteReader的问题
- Response.Write("<script>window.open('Storage_Print.aspx?condition="+this.lbl_condition.Text+"&str1="+this.str1.T...出错
- 各位帮个忙!如何将对象转化为Byte数组?
- C#连接sqlserver2005超时时间已到在操作完成之前超时时间已过或服务器未响应
- 再问一个字符串查找的问题
- 问个正则表达式
if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
{
bind();
}更多关于GridView操作,可参考 【清清月儿】 博客 http://www.cnblogs.com/aivdesign/articles/1236310.html