【问题标题】:Saving checked status from a checkbox into the database in asp.net将复选框中的选中状态保存到 asp.net 中的数据库中
【发布时间】:2013-06-10 20:34:50
【问题描述】:

我正在使用 asp.net 中的在线测试应用程序,并且在将检查的答案状态保存回数据库时遇到了麻烦,即一旦我单击我的 aspx 页面中的下一步按钮,这两件事应该会发生

1)它应该捕获当前选中的选项并将该特定选项的值作为 true 添加到我的数据库 IS_Marked 列中。

2)它还应该拉出下一组问题和选项

后者很好,但我不知道如何将检查的答案保存回数据库

我的 aspx 有 4 个复选框

而我的下一个按钮点击事件如下,

protected void BtnNext_Click(object sender, EventArgs e)

{

if (qid == maxid)//检查当前que id是否等于DB中最后一个que id

{
     BtnNext.Enabled = false; //if its last que then next button is disabled
}
else
{      
     BtnPrevious.Enabled = true; // if not last question next button is enabled

     QuestionSet q = new QuestionSet();  //Question set is an entity to hold que text and options list
     StudentB b = new StudentB(); //object of my business class
     q = b.GetQuestion(1, 1, qid, 'N', 0);//passing student id, test id, question id, action taken, i.e button clicked(prev, next, last, first) and selected question(i.e any question that is present)
     qid = Convert.ToInt32(q.Question_Id);

     LblQStn.Text = q.Question_Text;
     CheckBox1.Text = q.Options[0].Option_Text;//talking to business and model layer and getting que and ans from database and giving to checkboxes
     CheckBox2.Text = q.Options[1].Option_Text;
     CheckBox3.Text = q.Options[2].Option_Text;
     CheckBox4.Text = q.Options[3].Option_Text;
    }
}

现在,当他检查任何答案时,我需要将其状态作为标记保存到数据库中 任何解决方案都受到高度赞赏,

提前致谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    首先您需要建立与数据库的连接。例子: //sqlString 是你的连接字符串//

        dbconn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("sqlString"))
        dbconn.Open()
        strsql = "INSERT INTO IS_Marked ([Mark]) VALUES ('" + Bedrijf + "')"
        dbcomm = New SqlCommand(strsql, dbconn)
        dbcomm.ExecuteNonQuery()
        dbconn.Close()
    

    这将是您的基本 sql 命令。

    如您所见,我使用 ([yourvalue]) 我们接下来要做的事情。

        Dim dbconn As SqlConnection
        Dim dbcomm As SqlCommand
        Dim strsql, Checked_Mark As String
    
        Mark = YourCheckBox.CheckedValue
    

    所以你的最终结果会是这样的:

        Dim dbconn As SqlConnection
        Dim dbcomm As SqlCommand
        Dim strsql, Mark As String
    
        Mark = YourCheckBox.CheckedValue
        dbconn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("sqlString"))
        dbconn.Open()
        strsql = "INSERT INTO IS_Marked ([Mark]) VALUES ('" + Bedrijf + "')"
        dbcomm = New SqlCommand(strsql, dbconn)
        dbcomm.ExecuteNonQuery()
        dbconn.Close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多