我想输入一个班的学生成绩,先从数据库读出一个班的信息如姓名和学号,然后生成一张输入表,每行包括学生姓名和学号,以及一个成绩输入框.如下图:我应该用什么控件呢?gridview?reapter?,我现在是用了一个gridview,但是,那个输入框中的分数我如何获取?还有,分数不应该是字母什么的,我该如何验证它的有效性呢?对了,我想输入完成绩按回车键跳到下一个输入框,应该怎么操作?最后保存成绩的时候,如果有100个学生,是不是要100条update语句啊?可不可以循环更新呢?关键是每个班的学生数不固定,所以这些update语句怎么写呢?怎么把一个分数更新到对应的学生那条记录上啊?谢谢!---------------
题外话:发现这种情况下,还是asp灵活的多,取得记录集后在table中循环<tr>...
题外话:发现这种情况下,还是asp灵活的多,取得记录集后在table中循环<tr>...
解决方案 »
- Access insert values 为select值出错 查询输入必须包含至少一个表或查询
- 怎么利用 LinkButton 的 Click 和 command 属性实现双向排序
- 关于DropDownlist的诡异问题!急急急急!!!!!!!!!!
- 关于用户自定义控件的页面问题
- 关于gridview单元格的双击事件问题,在线等待!
- 请教Include的问题,小弟实在搞不懂了,大家都请进来看看。
- 请问如何判断某用户名是否已经注册?(绝对不是白痴问题)
- ???高手救命???
- C#中如何对字符型变量进行运算?
- 一个CS传参数的问题
- 跪求vs2008适用的分页控件!
- 正则 parten="<span>*****</span>"
foreach(GridViewRow gr in GridView1.Rows)
{
TextBox txt=gr.FindControl("txt1")as TextBox;}
可使用onblur等验证输入值或使用验证控件
GridView1_RowDataBound中
((TextBox)e.Item.FindControl("txt1")).Attributes["onkeydown"] = "if(event.keyCode==13){document.getElementById('" +txt.ClientID + "').focus();return false;};";
楼主可以试试看!
=============================
2,导入成功的在gridview 显示,不成功的也显示,
3,有个button 可以把成功的都保存到,数据里!批量的!
如果你的表名是ScoreSheet,主键是Id,字段是Name,Room,Seat,Score,那可以这样写:<%@ Page Language="C#" %><%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server"><!-- DataKeyNames填写表的主键,这样GridView就会记录每行的主键,在保存分数的时候可以知道该分数对应的学生 -->
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" DataKeyNames="Id" >
<Columns>
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:BoundField DataField="Room" HeaderText="考场号" />
<asp:BoundField DataField="Seat" HeaderText="座位号" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" onkeydown="if (event.keyCode==13) { return event.keyCode=9; }"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ControlToValidate="TextBox1" ValidationExpression="\d+"
ErrorMessage="分数必须是数字" Display="Dynamic" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><asp:Button ID="Button1" runat="server" Text="保存成绩" OnClick="Button1_Click" Enabled="false" /><!-- 下面的连接串和查询语言改成你的数据库情况 -->
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="server=localhost;uid=sa;pwd=xxx;database=xxx"
SelectCommand="select * from ScoreSheet">
</asp:SqlDataSource></form>
</body>
</html><script runat="server">
void Button1_Click(object sender, EventArgs e)
{
// 从数据库中读出成绩表
DataView dv = SqlDataSource1.Select(new DataSourceSelectArguments()) as DataView;
dv.Sort = "Id";
foreach (GridViewRow row in GridView1.Rows)
{
object id = GridView1.DataKeys[row.RowIndex].Value; // id是记录在GridView中的该行的主键
string score = (row.FindControl("TextBox1") as TextBox).Text;
dv[dv.Find(id)]["Score"] = score == "" ? 0 : Convert.ToInt32(score); // 该行中存入分数
} // 写回数据库
SqlDataAdapter adapter = new SqlDataAdapter();
string cmd = "update ScoreSheet set Score=@Score where Id=@Id";
adapter.UpdateCommand = new SqlCommand(cmd, new SqlConnection(SqlDataSource1.ConnectionString));
adapter.RowUpdating += delegate(object sender1, SqlRowUpdatingEventArgs e1)
{
e1.Command.Parameters.Clear();
e1.Command.Parameters.Add(new SqlParameter("Id", e1.Row["Id"]));
e1.Command.Parameters.Add(new SqlParameter("Score", e1.Row["Score"]));
};
adapter.Update(dv.Table); Response.Write("保存成功");
Response.End();
}</script>
btw, asp可以实现的方式,大多数在asp.net下面也可以用相似的方法实现,也就是说你可以按照写asp代码的思路来写asp.net程序,应该也能完成,不过这样就失去了asp.net的语言优势带来的编码的简洁高效。不要轻易下结论,等你了解了asp.net就会改变看法的。