php提交单选框到mysql,如何将单选按钮选项保存在数据库中

' id='' value='' />
--%>

public partial class Quiz : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!this.IsPostBack)

{

BindEmployeeData();

Repeater();

}

}

private void BindEmployeeData()

{

string connection = ConfigurationManager.AppSettings["Connection"].ToString();

using (SqlConnection con = new SqlConnection(connection))

{

using (SqlCommand objCommand = new SqlCommand("SELECT * FROM t_Question", con))

{

using (SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objCommand))

{

DataTable dt = new DataTable();

dt.Columns.Add("QId");

dt.Columns.Add("QText");

objSqlDataAdapter.Fill(dt);

Repeater0.DataSource = dt;

Repeater0.DataBind();

}

}

}

}

private void Repeater()

{

for (int i = 0; i <= Repeater0.Items.Count - 1; i++)

{

string connection = "server=sv01;database=testdb;uid=sa;password=mpx123";

Repeater Repeater1 = (Repeater)Repeater0.Items[i].FindControl("Repeater1");

HiddenField QID = (HiddenField)Repeater0.Items[i].FindControl("QID");

SqlConnection ak = new SqlConnection(connection);

ak.Open();

string query = "Select * from [dbo].[t_Answers] where QId=" + QID.Value;

SqlDataAdapter adp= new SqlDataAdapter(query, ak);

DataTable ck = new DataTable();

//ck.Columns.Add("AOptions");

adp.Fill(ck);

Repeater1.DataSource = ck;

Repeater1.DataBind();

ak.Close();

}

}

protected void BtnSubmit_Click(object sender, EventArgs e)

{

var collection = Repeater0.Items.Count;

foreach (var item in collection)

{

}

}

}

I have used two repeater in this, one for question,question Id and other for radio button to get data from database, but now i don't know how to save data that every radio button consist whenever user gonna check that option .

What I have tried:

I am trying for-each loop but don't know how to use it with nested repeater

protected void BtnSubmit_Click(object sender, EventArgs e)

{

var collection = Repeater0.Items.Count;

foreach (var item in collection)

{

}

}

解决方案

Simplest way is to read the answers from the Request. The "name" of your radio buttons are the question ID so if you Request.Form[QId] you'll get the selected answer

protected void BtnSubmit_Click(object sender, EventArgs e)

{

var collection = Repeater0.Items;

foreach (RepeaterItem item in collection)

{

HiddenField QID = (HiddenField)item.FindControl("QID");

int q = int.Parse(QID.Value);

string answer = Request.Form[q.ToString()];

}

}

An alternative would be to use server-side radio buttons, or a radiobuttonlist, and access those controls programmatically.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值