且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将单选按钮选项保存在数据库中

更新时间:2023-02-15 11:42:02

最简单的方法是从请求中读取答案。单选按钮的名称是问题ID,所以如果你Request.Form [QId]你会得到选定的答案



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()];

    }
}





另一种方法是使用服务器端单选按钮,或者radiobuttonlist,并以编程方式访问这些控件。



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