模态弹出窗口中的文本框插入重复的条目

时间:2011-11-14 21:03:58

标签: asp.net vb.net textbox duplicate-data modalpopup

我的文本框应该输入一个值并输入大约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

2 个答案:

答案 0 :(得分:1)

移出用于从每个周期插入新功能的代码。

答案 1 :(得分:1)

我认为这是因为:

If feature.Selected Then
 ...   
Else
  ....
End If

这意味着对于复选框列表中的每个项目,当未选中复选框时,它将始终进入Else块,这会导致多次插入。

额外建议

不要将您的UI代码和数据访问代码混合在同一个类/页面上。一个好的做法是将UI与业务逻辑从数据访问逻辑分离。例如,此代码:

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个参数的类:UserIDProductIDMarketingData。通过这样做,您可以在其他地方(例如页面)重复使用它,而不必一次又一次地重复逻辑。

最理想的情况是,您应该有一个调用数据访问层的业务层(上面的代码将进入您的数据访问层),但至少将此代码移到另一个地方是一个良好的开端。除了能够重用代码之外的另一个优点是假设您在此例程中检测到错误,您可以在一个地方修复它,而不是必须去应用程序上的不同位置来纠正错误。