Results 1 to 10 of 10

Thread: deleting records in DataList view

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    deleting records in DataList view

    Hi there.

    I have a datalist

    in this there are some fields (obviously) that displays records details.

    I have placed 2 button in it as well

    1) delete
    2) submit changes


    when the user hits delete I wish to delete that record. Each record has a unique ID number.

    The thing is, when i double click on the button, it doesnt displays it's click method, I guess its because im using ASP.NET and VB.NET etc...

    Is there a way to do this? even maybe if i create a public/private method, then in ASP.NET/HTML tell it to do the onClick thing and call that method?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I'm at the exact same point you are. I'm using a DataList control, and I've figured out how to load it, but haven't been able to save it, update, or delete anything.

    I did find this link, by the way:

    http://www.ondotnet.com/pub/a/dotnet.../datalist.html

    If it gives you a clue, then post your code and tell me how you did it.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    yeh i tried that but everytime i run it i get a compilation error. because I have put an onClick event in the asp.net/html code on the button

    everytime the page is loaded/executed i get the compilation error....

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Show the code you're using.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    hm

    button is on datalist

    then in asp/html:

    Code:
    ...
    ...
    
    <TD width="10%">
    					
    <asp:Button id="BtnSubmit" runat="server" Width="104px" Text="Submit Changes"></asp:Button>				
    <asp:Button id=deleteButton runat="server" Width="104px" Text="Delete Record" onClick = "DeleteRecord()" CommandName="delete" CommandArgument="<%# DataBinder.Eval(Container, &quot;DataItem['id']&quot; ) %>">		
    </asp:Button></TD>
    ...
    ...

    in vb.net, made this method:


    Code:
     Public Sub DeleteRecord(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    
            Response.Write("<br><br>skjgbsgbrg")
    
        End Sub

    error:


    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30455: Argument not specified for parameter 'e' of 'Public Sub DeleteRecord(sender As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs)'.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    sorted!!!!!


    in code:

    Code:
     Public Sub DeleteRecord(ByVal Source As Object, ByVal e As EventArgs)
    
    
    
        End Sub

    notice the params in the brackets


    then in asp.net:


    Code:
    onClick = "DeleteRecord"




    but now, how do i go about actually deleting the record?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    oh! try this one also when using a datalist:


    forget the onclick


    in asp.net:

    Code:
    OnUpdateCommand="UpdateRecord" OnDeleteCommand="DeleteRecord"
    [/code]

    in vb.net

    Code:
    Public Sub DeleteRecord(ByVal Source As Object, ByVal e As DataListCommandEventArgs)
    
            Response.Write("<br><br><br><br>Delete")
    
        End Sub
    
        Public Sub UpdateRecord(ByVal Source As Object, ByVal e As DataListCommandEventArgs)
    
            Response.Write("<br><br><Br><Br>Update")
    
        End Sub
    
    End Class

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    ****... looks like I've gotten something after all.

    VB Code:
    1. Dim int1 As Integer, int2 As Integer
    2.  
    3.         For int1 = 0 To DataList1.Items.Count - 1
    4.             For int2 = 1 To (DataList1.Items(int1).Controls.Count - 4) Step 2
    5.  
    6.                 Response.Write(CType(DataList1.Items(int1).Controls(int2), TextBox).Text.ToString)
    7.             Next
    8.         Next

    I do not know why I have to do a Step 2 in there. My setup is like this:

    4 textboxes, followed by 1 command button in each row.

    Apparently, the controls go in "2"s, so I have to do a step 2 there. This code retrieves the values. What you can do now, is to use this and update your dataset's rows or use a query.

    Also, what I had to do was to name the textboxes at the end of the row like this:

    VB Code:
    1. btn11.Text = e.Item.ItemIndex.ToString

    And so, I'd use the text of the button to determine which row I am. You can do the same:

    VB Code:
    1. Public Sub DeleteRecord(ByVal Source As Object, ByVal e As EventArgs)
    2.         Response.Write(CType(Source, Button).Text)
    3. ...

    See what I mean?

    This has to be one of the crudest piece-of-**** snippets of code I've written in my life. I feel like pouring steaming hot doggy poo over myself.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    lol thanks

    but that doesnt actually delete the record :P but i will try it for seeing exactly what it gets out

    but iget the error when using this code that table 0 could not be found!

    Code:
            Dim dr As DataRow
            dr = Ds1.Tables(0).Rows.Find(DataList1.DataKeys(e.Item.ItemIndex))
    
            dr.Delete()

    ??

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You can modify that code to get you the row() number and item() number in your dataset.

    I don't know why you're getting an error for Table(0), though. Have you given the table a name?

Posting Permissions

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



Click Here to Expand Forum to Full Width