急救~~~急救~~急救~~~急救~~~
大家好,我是个在学ASP.NET的新手,在做一个考试系统时遇到了困难,于是想请教下,不知是否可以给我点时间,帮我下!~~
我想在DataGrid的模板列里加入RadioButtonList,然后把从数据库中读取到的数据用RadioButtonList显示出来,最后按提交按钮后,把选择的信息和数据里的答案比较,如果正确就加分,错误就减分. 而我卡在了如何把数据库和RadioButtonList对应起来,最后如何比较? 如果你上线了,看到了,有时间的话,可以帮我下的话,我将万分感谢!
[解决办法]
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) BindGrid(); } private void BindGrid() { GridView1.DataSource = CreateData(); GridView1.DataBind(); } private DataTable CreateData() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("User name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Chinese name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("English name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("E-mail", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Sex", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Telephone", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Comments", System.Type.GetType("System.String"))); for (int i = 0; i < 30; i++) { DataRow dr = dt.NewRow(); dr["User name"] = "Msotec" + i.ToString(); dr["E-mail"] = "zcyhappy@msn.com"; dr["Chinese name"] = "微软技术论坛"; dr["English name"] = "Msotec"; dr["Telephone"] = "13864732837"; dr["Comments"] = "www.msotec.com"; dr["Sex"] = "1"; dt.Rows.Add(dr); } return dt; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindGrid(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = CreateData(); (e.Row.FindControl("rdoButtonSex") as RadioButtonList).Items.FindByValue(dt.Rows[e.Row.RowIndex]["Sex"].ToString()).Selected = true; } } protected void btnGet_Click(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { RadioButtonList rdoButton = new RadioButtonList(); rdoButton = (RadioButtonList)GridView1.Rows[i].Cells[6].FindControl("rdoButtonSex"); Response.Write("行号:" + i.ToString() + "RadioButtonList的值:" + rdoButton.SelectedItem.Text + "<br />"); } }