簡體   English   中英

復選框值未插入數據庫

[英]checkbox value not inserting in database

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:-這里是帶有復選框的 jquery script.dialogbox

    <script type="text/javascript">
                $(document).ready(function () {
                    $("#dialog").dialog({
                        buttons: {
                            Ok: function () {
                                $("[id*=Button1]").click();
                            },
                            Close: function () {
                                $(this).dialog('close');
                            }
                        }
                    });
                    });
            </script>
    

后面的代碼:- 使用 ado 和 sql 在數據庫中插入數據的文件后面的 c# 代碼

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

如果選中該復選框,它將僅將“N”保存到數據庫,然后還將“N”保存到數據庫。 如何解決這個問題?

我在我這邊調試了你的代碼,但沒有在代碼中發現任何問題。 每當我選中復選框時,我都會得到預期值Y

看來問題可能來自其他地方

請參閱隨附的我的調試屏幕截圖。

CHK1

在上面的屏幕截圖中,我選中了第一個checkbox ,在調試部分我得到它的值為Y

CHK2

建議:

嘗試將您的代碼帶到一個新頁面並實現這部分功能。 你會明白,這個問題來自其他地方。

希望這會有所幫助,如果您遇到與此相關的任何其他問題,請告訴我。

更新

由於您使用的是dialog box ,請查看以下鏈接以供參考。 它可能會幫助你

對話框

你的代碼沒問題。 分享你的 Sql 代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM