将文本框添加到模态弹出窗口已停止允许INSERTS

时间:2011-11-14 19:48:02

标签: asp.net vb.net textbox

我有一个模态弹出窗口,允许管理员从各种复选框中进行选择,以便为特定产品添加功能。如果有一个管理员认为应该可用但不是因为该功能不在数据库中的复选框,我添加了一个文本框,以便用户可以向产品添加新功能。

一旦我添加了文本框,就会停止允许插入。起初,它与每个循环的复选框在同一个If Else中,但我最近更改了它,因此文本框在每个循环中都是自己的。我在txtFeature.Text下面有一个下划线,上面写着Value of type 'Char' cannot be converted to 'System.Web.UI.WebControl.Textbox.'

vb代码:

  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
    Next
    For Each txtFeature As TextBox In txtFeature.Text
        If txtFeature.Text Then
            Dim featureSql As String = "INSERT INTO Marketing(ProductID, 
                                        MarketingTypeID, MarketingTitle, MarketingData) 
                                        VALUES (@ProductID, 3, 'Feature', 
                                        @MarketingData);
                                        UPDATE Product SET ModifyDate = getdate(), 
                                        ModifyUser = @ModifyUser 
                                        WHERE ProductID = @ProductID; 
                                        INSERT INTO Feature (FeatureTitle) 
                                        VALUES (@FeatureTitle)"

            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))

                    cn.Open()

                    cmd.ExecuteNonQuery()
                End Using

                cn.Close()
            End Using
        End If

ASPX:

<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><br />
             <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>

1 个答案:

答案 0 :(得分:1)

您正在尝试迭代字符串并将其中的每个字符强制转换为TextBox类:For Each txtFeature As TextBox In txtFeature.Text。显然你有错误。只需删除该周期,然后检查txtFeature.Text属性:If Not String.IsNullOrEmpty(txtFeature.Text) Then ...