如何将单选按钮选项保存在数据库中 [英] How to get radio buttons options to be saved in database

查看:205
本文介绍了如何将单选按钮选项保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:Repeater ID="Repeater0" runat="server">
          <ItemTemplate>
              <table>
                  <tr>
                      <td>
                           <div id="lblEmployeeId" ><%# Eval("QId")%>    <%# Eval("QText")%></div>
                          <asp:HiddenField ID="QID" runat="server" Value='<%# Eval("QId")%>'/>

                          <br />

                      </td>

                  </tr>
                  <tr class="table-row">
                      <td>

                          <asp:Repeater ID="Repeater1" runat="server">
                                   <ItemTemplate>
                               <div runat="server"> <input type="radio" name='<%#Eval("QID")%>'  value='<%#Eval("AOptions")%>' id="AnswerOptions"><%#Eval("AOptions")%></input></div>   <br />
                                    <%--   <asp:RadioButton ID="RadioButton1" runat="server" name='<%#Eval("QID")%>' id='<%#Eval("ID")%>' value='<%#Eval("AOptions")%>' /><%#Eval("AOptions")%><br />--%>
                                   </ItemTemplate>
                              </asp:Repeater>
                          <br />
                      </td>
                  </tr>
              </table>




























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)
        {
            
        }
    }



  
}





我在这里使用了两个中继器,一个用于提问,问题ID和其他用于单选按钮获取数据从数据库,但现在我不知道如何保存每个单选按钮组成的数据,只要用户要检查该选项。



我尝试过:



我正在尝试 - 每个循环但不知道如何将它与嵌套转发器一起使用





protected void BtnSubmit_Click(object sender,EventArgs e)

{

var collection = Repeater0.Items.Count; < br $>


foreach(收藏中的var项目)

{



}

}



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)
{

}
}

推荐答案

最简单的方法是从请求中读取答案。单选按钮的名称是问题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.


这篇关于如何将单选按钮选项保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆