是否可以在不提交的情况下保存数据?

时间:2015-03-26 09:16:09

标签: javascript c# asp.net listbox submit

我正在使用listbox1和listbox2,我使用了一个函数将listbox1的项目转移到listbox2反之亦然..现在我无法保存更改,因为当我单击一个带有runat =“server”的按钮时它会自动提交,我从listbox1转移到listbox2的项目将返回到listbox1,反之亦然,它会先读取按钮内的代码。

所以我想知道是否可以将项目保存到数据库而不提交..

但我不知道语法希望你能帮助我..

这是我的代码

<%@ Page Title="ListBox_v2" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DM_application.WebForm1" %>
<%@import Namespace="System.Data" %>
<%@import Namespace="System.Data.SqlClient" %>
<%@import Namespace="System.Configuration" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <%--•◘•◘•◘•◘Transfer•◘•◘•◘•◘•◘•◘--%>
    <script language="C#" runat="server">
           SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\index_DB.mdf;Integrated Security=True;User Instance=True");
           protected void SavingData(object sender, EventArgs e)
           {

               //███████████████████████████████  ◘DELETE◘  ███████████████████████████████
               conn.Open();
               SqlCommand cmd = new SqlCommand("Delete from [User_table_list] ", conn);
               cmd.ExecuteNonQuery();
               conn.Close();

               //███████████████████████████████   ◘ADD◘   ███████████████████████████████
               foreach (ListItem item in LeftBox.Items)
               {
                   conn.Open();
                   string[] splits = item.ToString().Split(new char[] { '-' });

                   SqlCommand SaveCommand = new SqlCommand("Insert Into User_table_list (ID, Firstname,Middlename,Lastname,Age,Sex) Values (@ID, @FName, @MName, @LName,@Age,@Sex)", conn);
                   SaveCommand.Parameters.Add("@ID", SqlDbType.NVarChar).Value = splits[0].ToUpper();
                   SaveCommand.Parameters.Add("@FName", SqlDbType.NVarChar).Value = splits[1].ToUpper();
                   SaveCommand.Parameters.Add("@MName", SqlDbType.NVarChar).Value = splits[2].ToUpper();
                   SaveCommand.Parameters.Add("@LName", SqlDbType.NVarChar).Value = splits[3].ToUpper();
                   SaveCommand.Parameters.Add("@Age", SqlDbType.NVarChar).Value = splits[4].ToUpper();
                   SaveCommand.Parameters.Add("@Sex", SqlDbType.NVarChar).Value = splits[5].ToUpper();
                   SaveCommand.ExecuteNonQuery();

                   conn.Close();
               }



               //███████████████████████████████ ◘DELETE◘ ███████████████████████████████
               //conn.Open();
               SqlCommand DeleteData = new SqlCommand("Delete from [User_table_list2] ", conn);
               //DeleteData.ExecuteNonQuery();
              // conn.Close();

               //███████████████████████████████ ◘ADD◘ ███████████████████████████████
               foreach (ListItem item in RIghtBox.Items)
               {
                   conn.Open();
                   string[] splits = item.ToString().Split(new char[] { '-' });

                   SqlCommand SaveCommand = new SqlCommand("Insert Into User_table_list2 (ID, Firstname,Middlename,Lastname,Age,Sex) Values (@ID, @FName, @MName, @LName,@Age,@Sex)", conn);
                   SaveCommand.Parameters.Add("@ID", SqlDbType.NVarChar).Value = splits[0].ToUpper();
                   SaveCommand.Parameters.Add("@FName", SqlDbType.NVarChar).Value = splits[1].ToUpper();
                   SaveCommand.Parameters.Add("@MName", SqlDbType.NVarChar).Value = splits[2].ToUpper();
                   SaveCommand.Parameters.Add("@LName", SqlDbType.NVarChar).Value = splits[3].ToUpper();
                   SaveCommand.Parameters.Add("@Age", SqlDbType.NVarChar).Value = splits[4].ToUpper();
                   SaveCommand.Parameters.Add("@Sex", SqlDbType.NVarChar).Value = splits[5].ToUpper();
                   SaveCommand.ExecuteNonQuery();

                   conn.Close();
               }
           }

           </script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#left").bind("click", function () {
                    var options = $("[id*=RIghtBox] option:selected");
                    for (var i = 0; i < options.length; i++) {
                        var opt = $(options[i]).clone();
                        $(options[i]).remove();
                        $("[id*=LeftBox]").append(opt);
                    } 
                });

                $("#right").bind("click", function () {
                    var options = $("[id*=LeftBox] option:selected");
                    for (var i = 0; i < options.length; i++) {
                        var opt = $(options[i]).clone();
                        $(options[i]).remove();
                        $("[id*=RIghtBox]").append(opt);

                    }
                });
            });


        </script>

   <script type="text/javascript">
       $(function () {
           $("[id*=SaveButton]").bind("click", function () {

               $("[id*=RIghtBox] option").attr("selected", "selected");
               $("[id*=LeftBox] option").attr("selected", "selected");
           });
       });
      </script>

      <%--<script type="text/javascript">

          $("#left").submit("Click", function () {
              return Button3_Click();
          });
          $("#right").submit("Click", function () {
              return Button2_Click();
          });
           
          </script>--%>
    <table class="style1" 
        style="height: 400px; width: 654px; position: static; z-index: auto;" 
        align="center">
        <tr>
            <td class="style6">
                <asp:Panel ID="Panel1" runat="server" Height="300px" Width="255px" 
                    align="center">
                    <span class="style9">LEFT </span>
                    <asp:ListBox ID="LeftBox" runat="server" Height="270px" Width="250px" 
                        SelectionMode="Multiple" AutoPostBack="true"></asp:ListBox>
                    <br />
                    <asp:Label ID="count_label1" runat="server" CssClass="style8"></asp:Label>
                </asp:Panel>
            </td>
            <td class="style7">
                <asp:Panel ID="Panel2" runat="server" Height="300px" Align="Center" 
                    Width="150px">
                    <br />
                    <br />
                    <br />
                    <br />
                    <input id="left" type="button" value="<<<" onclick="return left_onclick()"/>
                    <input id="right" type="button" value=">>>" onclick="return right_onclick()"/>
                    <br />
                    <br />
                    <asp:Button ID="SaveButton" runat="server" Text="Save Changes" 
                        Font-Bold="True" onclick="SaveButton_Click" />

                    <asp:Button ID="Button6" runat="server" Text="Button" onclick="SavingData"/>
                </asp:Panel>
            </td>
            <td>
                <asp:Panel ID="Panel3" runat="server" Height="300px" Width="252px" 
                    align="center">
                    <span class="style9">RIGHT</span>
                    <asp:ListBox ID="RIghtBox" runat="server" Height="270px" Width="250px" 
                        SelectionMode="Multiple" AutoPostBack="true"></asp:ListBox>
                    <br />
                    <asp:Label ID="count_label2" runat="server" CssClass="style8"></asp:Label>
                </asp:Panel>
            </td>
        </tr>
    </table>



</asp:Content>

0 个答案:

没有答案