2016-07-19 89 views
1

每當選中對話框中的複選框,並在點擊按鈕後,它不會將數據保存到數據庫它只保存'N',但是複選框被選中,然後它也保存'N'如何解決這個問題。如果複選框被選中,我想將'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 
+0

調試代碼一步一步檢查哪些_value_是你得到,而你選中該複選框 – BNN

回答

0

調試在我身邊你的代碼,但沒有找到代碼中的任何問題。每當我選中複選框時,我的預期值爲Y

看來這個問題可能來自某處其他地方。

查看我調試的附件截圖。

CHK1

在上面的截圖,我查了第一checkbox,我得到了它的價值在調試部分Y

CHK2

建議:

嘗試把你的代碼到一個新的新的一頁,並實現你的功能這一部分。你會得到一個想法,問題來自其他地方。

希望這可以幫助,讓我知道你是否面臨任何其他相關的問題。

UPDATE

由於您使用dialog box,看看下面的鏈接,供大家參考。它可以幫助你

Dialogbox

+0

在Web窗體我沒有得到任何錯誤,但如果我在對話框中使用的話,我得到的錯誤我需要在對話框中使用複選框 –

+0

你如何使用'dialog'對話框,代碼在哪裏?請更新您的代碼相同 – BNN

+0

所有html和jquery代碼在這裏請再次檢查 –