2011-11-14 10 views
1

私のテキストボックスは1つの値を入力し、同じものを8つ入力します。誰でも知っている理由は?モーダルポップアップのテキストボックスに重複した項目が挿入されます

<li class="item"> 
     <asp:LinkButton ID="FeatureButton" runat="server">Feature</asp:LinkButton> 
     <asp:Panel ID="FeaturePanel" runat="server" CssClass="modalPopup" 
     Style="display:none"> 
      <div class="PopupHeader">Add a Feature</div> 
      <asp:CheckBoxList ID="cbxAddFeature" runat="server" 
      DataSourceID="dsNewFeatures" DataTextField="FeatureTitle" 
      DataValueField="FeatureID"></asp:CheckBoxList> 
      New Feature:<asp:TextBox ID="txtFeature" runat="server"></asp:TextBox> 
      <asp:Label ID="FeatureError" runat="server" ></asp:Label> 
      <asp:Button ID="SubmitFeatures" runat="server" Text="Submit" /> 
      <asp:Button ID="CancelSubmitFeatures" runat="server" Text="Cancel" /> 
     </asp:Panel> 
     <asp:ModalPopupExtender ID="FeatureModal" runat="server" 
     BackgroundCssClass="modalBackground" 
     CancelControlID="CancelSubmitFeatures" DropShadow="True" 
     DynamicServicePath="" Enabled="True" PopupControlID="FeaturePanel" 
     TargetControlID="FeatureButton"></asp:ModalPopupExtender> 
Protected Sub SubmitFeatures_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles SubmitFeatures.Click 
    FeatureModal.Hide() 
    For Each feature As ListItem In cbxAddFeature.Items 
     If feature.Selected Then 
      'SQL INSERT: Marketing Table 
      Dim strSQL As String = "INSERT INTO Marketing (ProductID, 
            MarketingTypeID, MarketingTitle, MarketingData) 
            VALUES (@ProductID, 3, 'Feature', @MarketingData); 
            UPDATE Product SET ModifyDate = getdate(), 
            ModifyUser = @ModifyUser 
            WHERE ProductID = @ProductID" 

これは、チェックボックスの挿入も含むモーダルポップアップ全体のコードです。ユーザーに適切なチェックボックスが表示されない場合は、テキストボックスを使用して新しい値を入力できます。

Protected Sub SubmitFeatures_Click(ByVal sender As Object, ByVal e 
As System.EventArgs) Handles SubmitFeatures.Click 
    FeatureModal.Hide() 
    For Each feature As ListItem In cbxAddFeature.Items 
     If feature.Selected Then 

      Dim strSQL As String = "INSERT INTO Marketing (ProductID, 
            MarketingTypeID, MarketingTitle, MarketingData) 
            VALUES (@ProductID, 3, 'Feature', @MarketingData); 
            UPDATE Product SET ModifyDate = getdate(), 
            ModifyUser = @ModifyUser 
            WHERE ProductID = @ProductID" 

      Using cn As New SqlConnection 
      (System.Configuration.ConfigurationManager.ConnectionStrings 
      ("LocalSqlServer").ConnectionString) 

       Using cmd As New SqlCommand(strSQL, cn) 

        cmd.Parameters.Add(New SqlParameter("@ProductID", 
        ProductID.Value)) 
        cmd.Parameters.Add(New 
        SqlParameter("@MarketingData", feature.Value)) 
        cmd.Parameters.Add(New SqlParameter("@ModifyUser", 
        System.Web.HttpContext.Current.User.Identity.Name)) 

        cn.Open() 

        cmd.ExecuteNonQuery() 
       End Using 

       cn.Close() 
      End Using 
     Else 
     End If 
     If Not String.IsNullOrEmpty(txtFeature.Text) Then 
      Dim featureSql As String = "INSERT INTO Feature (FeatureTitle) 
             VALUES (@FeatureTitle); 
             INSERT INTO Marketing(ProductID, 
             MarketingTypeID, MarketingTitle, MarketingData) 
             VALUES (@ProductID, 3, 'Feature', 
             scope_identity()); 
             UPDATE Product SET ModifyDate = getdate(), 
             ModifyUser = @ModifyUser 
             WHERE ProductID = @ProductID" 

      Using cn As New SqlConnection 
      (System.Configuration.ConfigurationManager.ConnectionStrings 
      ("LocalSqlServer").ConnectionString) 

       Using cmd As New SqlCommand(featureSql, cn) 

        cmd.Parameters.Add(New SqlParameter("@FeatureTitle", 
        txtFeature.Text)) 
        cmd.Parameters.Add(New SqlParameter("@ProductID", 
        ProductID.Value)) 
        cmd.Parameters.Add(New SqlParameter("@ModifyUser", 
        System.Web.HttpContext.Current.User.Identity.Name)) 

        cn.Open() 

        cmd.ExecuteNonQuery() 
       End Using 

       cn.Close() 
      End Using 
     End If 
    Next 
    'keep tab active and redirect to same page 
    Session("ActiveTabIdx") = TabContainer1.ActiveTabIndex 
    Response.Redirect(Request.RawUrl) 
End Sub 

答えて

1

は、私はそれは、このためだと思う:

チェックボックス、リスト内のすべての項目のために、それは常に複数の挿入を引き起こしているチェックボックスが選択されていないElseブロックの中に入ることを意味します
If feature.Selected Then 
... 
Else 
    .... 
End If 

エクストラ提案

は、あなたのUIコードと同じクラス/ページ上のデータアクセスコードを混在させないでください。良い方法は、ビジネスロジックとUIをデータアクセスロジックから分離することです。例えば、このコード:UserIDProductIDMarketingData

Dim strSQL As String = "INSERT INTO Marketing (ProductID, 
            MarketingTypeID, MarketingTitle, MarketingData) 
            VALUES (@ProductID, 3, 'Feature', @MarketingData); 
            UPDATE Product SET ModifyDate = getdate(), 
            ModifyUser = @ModifyUser 
            WHERE ProductID = @ProductID" 

      Using cn As New SqlConnection 
      (System.Configuration.ConfigurationManager.ConnectionStrings 
      ("LocalSqlServer").ConnectionString) 

       Using cmd As New SqlCommand(strSQL, cn) 

        cmd.Parameters.Add(New SqlParameter("@ProductID", 
        ProductID.Value)) 
        cmd.Parameters.Add(New 
        SqlParameter("@MarketingData", feature.Value)) 
        cmd.Parameters.Add(New SqlParameter("@ModifyUser", 
        System.Web.HttpContext.Current.User.Identity.Name)) 

        cn.Open() 

        cmd.ExecuteNonQuery() 
       End Using 

       cn.Close() 
      End Using 

は、3つのパラメータを受信する別のクラスに完全に移動させることができます。これを繰り返すことで、ロジックを何度も繰り返す必要がなく、他の場所(ページなど)で再利用できます。

データアクセスレイヤーを呼び出すビジネスレイヤーが必要です(上のコードはデータアクセスレイヤーに入ります)が、少なくともこのコードを別の場所に移動することは良いスタートです。もう1つの利点は、コードを再利用できることに加えて、このルーチンでエラーを検出した場合、エラーを修正するためにアプリの別の場所に移動する必要がなく、1か所で修正できます。

+0

ありがとう! :)完璧に動作します – jlg

+0

@ jlg:私はいくつかの追加提案を追加しました。これはあなたのためのより多くの仕事を意味しますが、それは長期的に報酬を得るでしょう。私はちょうどgoogleのこのコードプロジェクトを見つけた:http://www.codeproject.com/KB/architecture/three_tier_architecture.aspxそれはあなたに私が意味するもののよりよい考えを与えるかもしれません:) – Icarus

+0

良いアイデア。私はすべてのものを聞いたことがあり、次のプロジェクトでそれを試してみます。このサイトは来週にライブになる予定だから、再学習するのに十分な時間があるかどうかはわかりません。それでもやはり大きな障害が1つあります。 :)ありがとう。 – jlg

1

各サイクルごとに新しいフィーチャを挿入するコードを移動します。

+0

ありがとう、それは完全に動作します。 – jlg

関連する問題