更新不能起作用...
感觉论谈里面的这种无名的方式的互动很令信欣慰....我在一个更新页面中使用UPDATE.但是它没有起作用...大家可以看一下发表一下哈...
代码如下:
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true"%>
<script language="C#" runat="server">
string strconn;
string querystr;
SqlConnection nwconn;
SqlDataAdapter stuadapter;
DataSet stuset=new DataSet();
void Page_Load()
{
ConnectData();
querystr="select * from stu";
stuadapter=new SqlDataAdapter(querystr,nwconn);
stuadapter.Fill(stuset,"stu");
BindGrid();
}
void ConnectData()
{
strconn="user id=sa;password=sql;database=datac;";
nwconn=new SqlConnection(strconn);
}
void BindGrid()
{
stugrid.DataSource=stuset;
stugrid.DataBind();
}
public void stugrid_Edit(object sender,DataGridCommandEventArgs dgcea)
{
stugrid.EditItemIndex=dgcea.Item.ItemIndex;
BindGrid();
}
public void stugrid_Update(object sender,DataGridCommandEventArgs dgcea)
{
int rowcount=dgcea.Item.ItemIndex;
TextBox tbox=(TextBox)dgcea.Item.FindControl("sno");
stuset.Tables["stu"].Rows[rowcount]["sno"]=tbox.Text;
tbox=(TextBox)dgcea.Item.FindControl("sname");
stuset.Tables["stu"].Rows[rowcount]["sname"]=tbox.Text;
tbox=(TextBox)dgcea.Item.FindControl("data");
stuset.Tables["stu"].Rows[rowcount]["data"]=tbox.Text;
tbox=(TextBox)dgcea.Item.FindControl("computer");
stuset.Tables["stu"].Rows[rowcount]["computer"]=tbox.Text;
tbox=(TextBox)dgcea.Item.FindControl("english");
stuset.Tables["stu"].Rows[rowcount]["english"]=tbox.Text;
tbox=(TextBox)dgcea.Item.FindControl("software");
stuset.Tables["stu"].Rows[rowcount]["software"]=tbox.Text;
ConnectData();
SqlCommandBuilder updatebuilder=new SqlCommandBuilder(stuadapter);
stuadapter.UpdateCommand=updatebuilder.GetUpdateCommand();
stuadapter.Update(stuset,"stu");
stuset.Clear();
stuadapter.Fill(stuset,"stu");
BindGrid();
}
public void stugrid_Cancel(object sender,DataGridCommandEventArgs dgcea)
{
stugrid.EditItemIndex=-1;
stugrid.DataBind();
}
</script>
<html><head><title>学生成绩管理_修改成绩系统后台</title></head>
<body bgcolor="#CACACA" text="green" link="#ffff00" vlin="#ffff00" alink="#00ff00">
<center><h4>学生成绩系统后台管理:</h4></center>
<form runat="server">
<asp:datagrid id="stugrid" runat="server" AutoGenerateColumns="false"
OnEditCommand="stugrid_Edit" OnUpdateCommand="stugrid_Update" OnCancelCommand="stugrid_Cancel ">
<Columns>
<asp:TemplateColumn HeaderText="stuID">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"stuID")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=stuIDbox
Text='<%#DataBinder.Eval(Container.DataItem,"stuID")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="姓名">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"sname")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=sname
Text='<%#DataBinder.Eval(Container.DataItem,"sname")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="学号">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"sno")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=sno
Text='<%#DataBinder.Eval(Container.DataItem,"sno")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="数据库">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"data")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=data
Text='<%#DataBinder.Eval(Container.DataItem,"data")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="计算机操作系统">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"computer")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=computer
Text='<%#DataBinder.Eval(Container.DataItem,"computer")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="专业英语">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"english")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=english
Text='<%#DataBinder.Eval(Container.DataItem,"english")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="软件工程">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"software")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=software
Text='<%#DataBinder.Eval(Container.DataItem,"software")%>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="编辑"
>
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>
<br/><br/><br/><br/><br/><br/><font color="red"><hr height="3"></font>
<center>
<a href="insert.aspx">点此进入学生成本录入系统</a>
</center>
</form>
</body>