【问题标题】:checkbox value not inserting in database复选框值未插入数据库
【发布时间】:2016-11-22 07:05:26
【问题描述】:

whenever the checkbox in dialog box is checked and after clicking the button it doesn't save the data to database its only save 'N' but is checkbox is checked then also it saves 'N' how to solve this problem.如果选中复选框,我想将“Y”保存到数据库。

HTML:-这是声明复选框的html部分

<form id="form1" runat="server">
    <div id="dialog">
    <label>First</label>
      <asp:CheckBox ID="CheckBox1" runat="server" />
      <label>Second</label>
      <asp:CheckBox ID="CheckBox2" runat="server" />
      <label>Third</label>
      <asp:CheckBox ID="CheckBox3" runat="server" />

    </div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button2_Click" />

    </form>

Jquery:-here is the jquery script.dialogbox with checkbox

    <script type="text/javascript">
                $(document).ready(function () {
                    $("#dialog").dialog({
                        buttons: {
                            Ok: function () {
                                $("[id*=Button1]").click();
                            },
                            Close: function () {
                                $(this).dialog('close');
                            }
                        }
                    });
                    });
            </script>
    Code Behind:- c# code behind file inserting data in database using ado and sql

    protected void Button2_Click(object sender, EventArgs e)
            {

                ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('Button Clicked')", true);


                string First = CheckBox1.Checked ? "Y" : "N";
                string Second = CheckBox2.Checked ? "Y" : "N";
                string Third = CheckBox3.Checked ? "Y" : "N";

                string cs = ConfigurationManager.ConnectionStrings["DUM01ConnectionString"].ConnectionString;
                using (SqlConnection scon = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("insert into Checkbox (First,Second,Third) values(@First,@Second,@Third)"))
                    {
                        cmd.Connection = scon;
                        cmd.Parameters.AddWithValue("@First", First);
                        cmd.Parameters.AddWithValue("@Second", Second);
                        cmd.Parameters.AddWithValue("@Third", Third);
                        scon.Open();
                        cmd.ExecuteNonQuery();
                        scon.Close();
                    }
                }
            }


   it saves only 'N' to database if checkbox is checked then also it saves 'N' to database how to solve

【问题讨论】:

  • 逐步调试代码并检查您在选中复选框时得到的

标签: c# jquery sql asp.net ado.net


【解决方案1】:

调试了您的代码,但没有在代码中发现任何问题。每当我选中复选框时,我得到的预期值为Y

看来问题可能来自其他地方

查看附件中我的调试截图。

在上面的截图中,我检查了第一个checkbox,在调试部分我得到了Y的值。

建议:

尝试将您的代码带到一个新页面并实现这部分功能。您会发现问题出在其他地方。

希望这会有所帮助,如果您遇到与此相关的任何其他问题,请告诉我。

更新

由于您使用的是dialog box,请查看以下链接以供参考。可能对你有帮助

Dialogbox

【讨论】:

  • 在网络表单上我没有收到任何错误,但如果我在对话框中使用,那么我会收到错误,我需要在对话框中使用复选框
  • 你如何使用dialog 框,代码在哪里。?请更新您的代码
  • 所有html和jquery代码都在这里,请再次检查
猜你喜欢
  • 1970-01-01
  • 2014-01-23
  • 2010-09-19
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多