怎么样使用GridView控件一次性批量输入成绩到数据库
发表在ASP.NET图书答疑
2014-06-26
是否精华
是
否
版块置顶:
是
否
你好,我在做一个成绩管理系统,其中学生的学号课程号是从选课表里面读取,我想实现 其中一门课程下的学生的成绩能一次性批量输入,而不是GridView中一个一个修改,谢谢解决。其中前台部分代码如下: <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged">//分类显示 课程
</asp:DropDownList>
</p>
<p style=" color:Red; font-size:13px;">备注:选择00000</p>
</div>
<asp:GridView ID="Gridview1" runat="server" AllowPaging="false" DataKeyNames="stuID" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="stuID" HeaderText="学号" />
<asp:BoundField DataField="courseName" HeaderText=" 课程" />
<asp:TemplateField HeaderText="成绩">
<ItemTemplate>
<asp:TextBox ID="txtScore" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
</div>
后台: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = this.DropDownList1.SelectedValue;
string str = "SELECT [stuID],[courseID],[teaID],[semester],[courseName],[score]FROM [SelectCourse].[dbo].[Elect] where courseID='" + value + "'and score is null";
DataSet ds = du.executeQuery(str);
Gridview1.DataSource = ds;
Gridview1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//这里是批量输入,我查了很多资料具体不知道怎么弄了
{
StringBuilder query = new StringBuilder();
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
string va = this.DropDownList1.SelectedValue;
GridViewRow row = Gridview1.Rows[i];
string value = ((TextBox)row.Cells[5].FindControl("txtScore")).Text.Replace("", "");
string value1 = Gridview1.DataKeys[i].Value.ToString();
query.Append("UPDATE [SelectCourse].[dbo].[Elect]SET[score]='")
.Append(value1).Append("'WHERE[courseID]=''"+va+"'");
}
}
</asp:DropDownList>
</p>
<p style=" color:Red; font-size:13px;">备注:选择00000</p>
</div>
<asp:GridView ID="Gridview1" runat="server" AllowPaging="false" DataKeyNames="stuID" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="stuID" HeaderText="学号" />
<asp:BoundField DataField="courseName" HeaderText=" 课程" />
<asp:TemplateField HeaderText="成绩">
<ItemTemplate>
<asp:TextBox ID="txtScore" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
</div>
后台: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = this.DropDownList1.SelectedValue;
string str = "SELECT [stuID],[courseID],[teaID],[semester],[courseName],[score]FROM [SelectCourse].[dbo].[Elect] where courseID='" + value + "'and score is null";
DataSet ds = du.executeQuery(str);
Gridview1.DataSource = ds;
Gridview1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//这里是批量输入,我查了很多资料具体不知道怎么弄了
{
StringBuilder query = new StringBuilder();
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
string va = this.DropDownList1.SelectedValue;
GridViewRow row = Gridview1.Rows[i];
string value = ((TextBox)row.Cells[5].FindControl("txtScore")).Text.Replace("", "");
string value1 = Gridview1.DataKeys[i].Value.ToString();
query.Append("UPDATE [SelectCourse].[dbo].[Elect]SET[score]='")
.Append(value1).Append("'WHERE[courseID]=''"+va+"'");
}
}