+ Reply to Thread
Results 1 to 2 of 2

Thread: Asp : Getting value from a textBox in Datalist

  1. #1

    Asp : Getting value from a textBox in Datalist

    This is my datalist
    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
                    <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("ID") %>'  Visible="False" />
                    <asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
                    <br />
                    <asp:Label ID="DescriptionLabel" runat="server" 
                        Text='<%# Eval("Description") %>' />
                    <br />
                    <asp:Image ID="Image1" runat="server" 
                     ImageUrl='<%# Eval("ID", "Handler.ashx?ID={0}") %>' Width="200" Height="200"/>
                    <br />
                    <asp:Label ID="commentsLabel" runat="server" Text='<%# Eval("comments") %>' />
                    <br />
                    Ajouter commentaire 
                    <asp:button ID="btnAjouter"  runat="server" OnCommand="Button_Command"
                     CommandName="add" Text="Ajouter" />
                    <asp:TextBox ID="TextBoxComments" runat="server"></asp:TextBox>
    aspx.vb button event
    Sub Button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim connectionString As String = WebConfigurationManager.ConnectionStrings("BecsEtMuseauxSQL").ConnectionString
        Dim con As SqlConnection = New SqlConnection(connectionString)
        Dim cmd As New SqlCommand("updateComments", con)
        cmd.CommandType = CommandType.StoredProcedure
        //I try this ....
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = DataList1.FindControl("IdLabel").ToString()
        cmd.Parameters.Add("@Comments", SqlDbType.NVarChar).Value = DataList1.FindControl("TextBoxComments").ToString()
    End Sub
    I get an error like this "The object reference is not definied to an object instance"

    How to catch the value from the label IdLabel and TextBoxComments in this situation

  2. #2
    You should handle the DataList's ItemCommand instead of the Button's Command.
    Private Sub DataList1_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) _
        Handles DataList1.ItemCommand
        If e.CommandName = "add" Then
            Dim IdLabel = DirectCast(e.Item.FindControl("IdLabel"), Label)
            Dim TextBoxComments = DirectCast(e.Item.FindControl("TextBoxComments"), TextBox)
        End If
    End Sub
    On aspx, remove your redundant Command-handler:
    <asp:button ID="btnAjouter" CommandName="add"  Text="Ajouter" runat="server" />
    If you want to use the Button's Click-Event instead, that's possible also.
    Protected Sub btnAjouter_Click(sender As Object, e As EventArgs)
        Dim container = DirectCast(DirectCast(sender, Control).NamingContainer, DataListItem)
        Dim IdLabel = DirectCast(container.FindControl("IdLabel"), Label)
        Dim TextBoxComments = DirectCast(container.FindControl("TextBoxComments"), TextBox)
    End Sub
    Then add the click-event handler to the button:
    <asp:button ID="btnAjouter" OnClick="btnAjouter_Click" Text="Ajouter" runat="server" />

+ Reply to Thread

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts