我正在开发一个项目,我将Radiobutton绑定到网格视图,它可以工作并再次正确地将数据提取到GridView我想要将已检查和未检查的RadioButton值插入数据库,但是不能正常工作它将所有RadioButton传递为false。 澄清波纹管是它的代码。 从我感谢你的回答之前。
背后的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace Online_Examination_System
{
public partial class FormAnswers : System.Web.UI.Page
{
int count;
ClassCommon obj = new ClassCommon();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
count = 1;
}
DataTable dt = new DataTable();
dt = obj.GetData("select Question from tblQuestions where QId='" + count + "'");
gvQuestions.DataSource = dt;
gvQuestions.DataBind();
DataTable dt2 = new DataTable();
dt2 = obj.GetData("select Answer from tblAnswers where QId='" + count + "'");
gvAnswers.DataSource = dt2;
gvAnswers.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
count = 1;
if (ViewState["view"] != null)
{
count = Convert.ToInt16(ViewState["view"]);
}
ViewState["view"] = count + 1;
DataTable dt = new DataTable();
dt = obj.GetData("select Question from tblQuestions where QId='" + ViewState["view"] + "'");
gvQuestions.DataSource = dt;
gvQuestions.DataBind();
DataTable dt2 = new DataTable();
dt2 = obj.GetData("select Answer from tblAnswers where QId='" + ViewState["view"] + "'");
gvAnswers.DataSource = dt2;
gvAnswers.DataBind();
if (!IsPostBack)
{
for (int i = 1; i < gvAnswers.Rows.Count; i++)
{
int V;
RadioButton rBtnAnswer = gvAnswers.Rows[i].FindControl("rbtnSelect") as RadioButton;
if (rBtnAnswer.Checked)
{
V = 1;
}
else
{
V = 0;
}
Label txtAnswered = gvAnswers.Rows[i].FindControl("lblAnswer") as Label;
obj.ExeQuery("insert into tblResult values('" + V + "','" + txtAnswered.Text + "',Null,NULL,NULL,Null)");
}
}
}
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormAnswers.aspx.cs" Inherits="Online_Examination_System.FormAnswers" %>
<!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">
<head runat="server">
<script type="text/javascript">
function SelectRadioButton(radio) {
var rdBtn = document.getElementById(radio.id);
var rdBtnList = document.getElementsByTagName("input");
for (i = 0; i < rdBtnList.length; i++) {
if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id) {
rdBtnList[i].checked = false;
}
}
}
</script>
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style1">
<asp:GridView ID="gvQuestions" runat="server" GridLines="None">
</asp:GridView>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">
<asp:GridView ID="gvAnswers" runat="server" GridLines="None" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:RadioButton ID="rbtnSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<asp:Label ID="lblAnswer" runat="server" Text='<%# Bind("Answer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">
<asp:Button ID="btnSave" runat="server" Text="Next" OnClick="btnSave_Click" />
</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
以下代码永远不会被执行,因为它在!IsPostBack
条件内。
for (int i = 1; i < gvAnswers.Rows.Count; i++)
{
int V;
RadioButton rBtnAnswer = gvAnswers.Rows[i].FindControl("rbtnSelect") as RadioButton;
if (rBtnAnswer.Checked)
{
V = 1;
}
else
{
V = 0;
}
Label txtAnswered = gvAnswers.Rows[i].FindControl("lblAnswer") as Label;
obj.ExeQuery("insert into tblResult values('" + V + "','" + txtAnswered.Text + "',Null,NULL,NULL,Null)");
}
您需要做的就是删除!IsPostBack
条件。
有关详细信息,请查看this。