+ 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
    Code:
    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
                   RepeatLayout="Flow">
                <ItemTemplate>
    
                    <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("ID") %>'  Visible="False" />
                    Titre:
                    <asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
                    <br />
                    Description:
                    <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 />
                    comments:
                    <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>
                    <br/>
                    <br/>
                </ItemTemplate>
            </asp:DataList>
    aspx.vb button event
    Code:
    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)
    
        con.Open()
    
        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()
    
        cmd.ExecuteNonQuery()
    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.
    Code:
    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:
    Code:
    <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.
    Code:
    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:
    Code:
    <asp:button ID="btnAjouter" OnClick="btnAjouter_Click" Text="Ajouter" runat="server" />

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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