文本框不会更新数据库

时间:2011-11-01 18:51:14

标签: asp.net vb.net

我无法让我的文本框更新数据库。这些更改看起来像是保存的(它们保存在页面上),模式弹出窗口消失了,但我试图在数据库中更改的文本保持不变。

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim myControl As Control = FindControl("txtData")
    If (Not myControl Is Nothing) Then
        Dim UpdateSql As String = "UPDATE Picklist SET (Data) = @Data WHERE PicklistID = @PicklistID"
        Using cn As New SqlConnection
        (System.Configuration.ConfigurationManager.ConnectionStrings
        ("LocalSqlServer").ConnectionString)
            Using sqlcmd As New SqlCommand(UpdateSql, cn)
                sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl))
                cn.Open()
                sqlcmd.ExecuteNonQuery()
            End Using
        End Using
    Else
    End If
End Sub


 <asp:TabPanel ID="tab2"  runat="server" HeaderText="Descriptions">
<HeaderTemplate>Descriptions</HeaderTemplate>
    <ContentTemplate>
        <ul class="info">
        <asp:ListView ID="lvDescriptions" runat="server" 
          DataSourceID="dsAdminMarketingDescriptions" DataKeyNames="MarketingID">

        <ItemTemplate>
            <li class="item">
                <asp:LinkButton ID="ViewDescriptionButton" runat="server">
                <%# Eval("Title")%>
                </asp:LinkButton>

               <asp:ImageButton ID="DeleteDescriptionButton" runat="server"  
                Style="float:right;" AlternateText="" 
                ImageUrl="../../images/delete.png" CommandName="Delete" 
                OnClientClick="return confirm('Are you sure you want to delete this 
                description?')" />

                <asp:Panel ID="ViewDescriptionPanel" runat="server"
                  CssClass="DescModalPopup">                                   
                <div class="PopupHeader">View Description -- <%#Eval("Title") %>
                <asp:ImageButton ID="CancelDescriptionButton" runat="server" 
                  ImageUrl="../../images/cancel.png" AlternateText="" 
                  Style="float:right;"/>
                 <asp:ImageButton ID="EditDescriptionButton" runat="server" 
                   ImageUrl="../../images/edit.png" AlternateText="" 
                   Style="float:right;" CommandName="Edit" AutoPostBack="false" />
                </div>

                    <asp:Label ID="Description" runat="server" style="padding:2px;">
                    <%# Eval("Data")%>
                    </asp:Label>
                </asp:Panel> 

                <asp:ModalPopupExtender ID="ViewDescriptionModal" runat="server" 
                  BackgroundCssClass="modalBackground" DropShadow="false" 
                  DynamicServicePath="" Enabled="true" 
                  PopupControlID="ViewDescriptionPanel" 
                  TargetControlID="ViewDescriptionButton" 
                  CancelControlID="CancelDescriptionButton"></asp:ModalPopupExtender> 


                <asp:Panel ID="EditDescriptionPanel" runat="server" 
                  CssClass="DescModalPopup">

                <div class="PopupHeader">Edit Description -- <%# Eval("Title")%>
                <asp:ImageButton ID="Cancel" runat="server" 
                  ImageUrl="../../images/cancel.png" AlternateText="" 
                  Style="float:right;"/>
                </div>

                    <asp:TextBox ID="txtData" runat="server" TextMode="MultiLine" 
                     Text='<%# Eval("Data")%>'>
                    </asp:TextBox><br />
                <asp:Button ID="SubmitEdit" runat="server" Text="Submit" />
                <asp:Button ID="CancelEdit" runat="server" Text="Cancel" />
                </asp:Panel>

                <asp:ModalPopupExtender ID="EditDescriptionModal" runat="server" 
                BackgroundCssClass="modalBackground" DropShadow="false" 
                DynamicServicePath="" Enabled="true" 
                PopupControlID="EditDescriptionPanel" 
                TargetControlID="EditDescriptionButton">
                </asp:ModalPopupExtender>            
            </li>
        </ItemTemplate>

更新:我尝试使用try catch来收到错误消息,但它无效。

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim myControl As Control = FindControl("txtData")
    If (Not myControl Is Nothing) Then
        Try
            Dim UpdateSql As String = "UPDATE Picklist 
                                       SET (Data) = @Data 
                                       WHERE Title = @Title"
            Using cn As New SqlConnection
             (System.Configuration.ConfigurationManager.ConnectionStrings
             ("LocalSqlServer").ConnectionString)
                Using sqlcmd As New SqlCommand(UpdateSql, cn)
                    sqlcmd.Parameters.Add(New SqlParameter("@Data", 
                     DirectCast(myControl, TextBox).Text))
                    cn.Open()
                    sqlcmd.ExecuteNonQuery()
                End Using
                cn.Close()
            End Using
        Catch ex As Exception
            Response.Write(ex.Message())
        End Try
    Else
    End If
    Response.Redirect(Request.RawUrl)
End Sub

更新:想出来。这是后面的代码,所有其他代码保持不变

Protected Sub SubmitEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
    For Each item As ListViewDataItem In lvDescriptions.Items
        Dim txtData As TextBox = DirectCast(item.FindControl("txtData"), TextBox)
        Dim ltlTitle As Literal = DirectCast(item.FindControl("ltlTitle"), Literal)
        Dim UpdateSql As String = "UPDATE Picklist 
                                   SET Data = @Data 
                                   WHERE Title = @Title"
        Using cn As New SqlConnection
        (System.Configuration.ConfigurationManager.ConnectionStrings
        ("LocalSqlServer").ConnectionString)
            Using sqlcmd As New SqlCommand(UpdateSql, cn)
                sqlcmd.Parameters.Add(New SqlParameter("@Data", txtData.Text))
                sqlcmd.Parameters.Add(New SqlParameter("@Title", ltlTitle.Text))

                cn.Open()
                sqlcmd.ExecuteNonQuery()

            End Using
            cn.Close()
        End Using
    Next
    Response.Redirect(Request.RawUrl)
End Sub

1 个答案:

答案 0 :(得分:3)

您需要更改一行:

sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl))

sqlcmd.Parameters.Add(New SqlParameter("@Data", myControl.Text))

<强>更新

由于文本框位于listview itemtemplate中,因此您还需要通过查看当前选定的列表项来更改其发现方式。为此,请更改以下行:

Dim myControl As Control = FindControl("txtData")

为:

Dim myControl As TextBox = DirectCast(lvDescriptions.Items(lvDescriptions.SelectedIndex).FindControl("txtData"), TextBox)

我还更新了sqlParameters赋值以删除DirectCast,因为我们现在将控件声明为TextBox(但仍需要更新以使用原始代码中的Text属性)。