Pergunta

I have a problem with inserting data inserting data into database.

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"   OnItemDataBound="Repeater1_ItemDataBound">
   <ItemTemplate> 
     <!-- Some other data (text...) -->

     <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("PostId") %>' />  

     <asp:TextBox ID="txtAddComment" runat="server" CssClass="textbox"  Width="200px" />
     <asp:Button ID="btnAddComment" runat="server" CssClass="button" Text="Comment"   CausesValidation="false" OnClick="btnAddComment_Click"/>
   </ItemTemplate>
</asp:Repeater>

Code behind:

 Protected Sub btnAddComment_Click(sender As Object, e As EventArgs)
        Dim HiddenField1 As HiddenField = DirectCast(Repeater1.Items(0).FindControl("HiddenField1"), HiddenField)
        Dim txtAddComment As TextBox = DirectCast(Repeater1.Items(0).FindControl("txtAddComment"), TextBox)
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString.ToString)
        Try
            If txtAddComment.Text = "" Then
                Exit Sub
            Else
                conn.Open()
                Dim cmd As SqlCommand = conn.CreateCommand()
                cmd.CommandText = "INSERT INTO Comments (PostId, UserName, CommentText) VALUES (@PostId, @UserName, @Text)"

                cmd.Parameters.Add("PostId", System.Data.SqlDbType.Int).Value = CInt(HiddenField1.Value)
                cmd.Parameters.Add("UserName", System.Data.SqlDbType.NVarChar).Value = Context.User.Identity.Name
                cmd.Parameters.Add("Text", System.Data.SqlDbType.NText).Value = MakeLink(HtmlRemoval.StripTagsCharArray(txtAddComment.Text))                    
                cmd.ExecuteNonQuery()            
            End If

        Catch ex As SqlException                
        Finally
            conn.Close()
        End Try
    End Sub

I display some text, under the text there is textbox and button for comments. I am bounding textId in hiddenfield for inserting comments. The code works fine, but I can only add comment for the first row in database displayed by repeater. When I want to add comment for other rows (2, 3...), the page refreshes and the text in the TextBox stay there.

It is problem with this line of code:

Dim txtAddComment As TextBox = DirectCast(Repeater1.Items(0).FindControl("txtAddComment"), TextBox)

The ID from HiddenField is right. But the code doesn't perform because the line above references for the first textbox on the page and his value is null.

When I change the line of code like this:

  Dim txtAddComment As TextBox = DirectCast(Repeater1.FindControl("txtAddComment"), TextBox)

It returns an error that Object reference not set to an instance of an object.

How to get the right textbox with right value? Thanks for the answer

Foi útil?

Solução

cast the sender to a button that sent it, then get the textbox from the parent. Example:

Protected Sub button_click(ByVal sender As Object, ByVal e As EventArgs)
   Dim myButton As button= CType(sender, button)
   Dim myTextBox as TextBox = CType(myButton Parent.FindControl("buttonName"), TextBox)

end sub
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top