Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2008, 01:50 PM
Authorized User
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default datagrid command buttons

Hello,
I have a datagrid that is connected to ms-access DB.
I would like to make a delete link for each row, that clicking on that link will delete its value from the DB

I tried to add "ondelete" command,
and add a linkbutton to the templatecoulm, but that script is never executed when i click!

How can i call a sub when i click on a button in the templatecoulm?


thsnk you !

 
Old November 18th, 2008, 05:54 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Just curious -- why are you using a DataGrid instead of a GridView?

_________________________________

Visit my blog at http://leedumond.com
 
Old November 19th, 2008, 03:29 AM
Authorized User
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what is greedview?

 
Old November 19th, 2008, 03:58 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

The GridView is the thing that has replaced the DataGrid since ASP.NET 2.0.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(VS.80).aspx

You should not be using the DataGrid any more, unless you're still on 1.0. Use a GridView (or possibly a ListView, if you're working in 3.5).

_________________________________

Visit my blog at http://leedumond.com
 
Old November 22nd, 2008, 12:19 PM
Authorized User
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there,
I switched to GridView.

Can you explain me please how do i get the value of a specific field in the row that i select\button click on it?

thank you
 
Old November 22nd, 2008, 01:28 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You would handle the SelectedIndexChanged event of the GridView.

You wanted to know how to get the value of a specific field in a selected row, right?

Okay, let's say you want the value of something in the third column. That would be index #2 in the cells collection of that row (remember, the index starts at 0).

So, you'd go like this:

Code:
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
     Handles GridView1.SelectedIndexChanged
        Dim row As GridViewRow = GridView1.SelectedRow
        Label1.Text = "You selected " & row.Cells(2).Text & "!"
    End Sub
The above code would take whatever is displayed in the third column of the selected row, and display it in a label. Of course, you could do whatever you need to with that value.

_________________________________

Visit my blog at http://leedumond.com
 
Old November 22nd, 2008, 02:50 PM
Authorized User
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yjank you,

Does it work with the rowdeleting event?

But counting the cells sounds premitive.

|Imagine you have a table with 100 fields..
You need to count each cell till you get the one you want?
 
Old November 22nd, 2008, 10:18 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

I'm not sure what you're asking.

You can easily create a CommandField with a Delete button in it. But are you asking if it's possible to get the value of a field in a row you are deleting?



_________________________________

Visit my blog at http://leedumond.com
 
Old November 23rd, 2008, 06:34 AM
Authorized User
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there.

I need to create a rowdeleting event,
in that event i have to create a sql statment that deleted the record from the db.

I have an invisible field with the rowID of every row displayed.

I need to get that ID when i click on the delete button...



 
Old November 23rd, 2008, 11:38 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You don't have to do any of that when deleting records, as the Gridview supports deleting records directly through the datasource to which it is bound.

Let's say you had a list of products, and the primary key is called ProductID. By specifying the DataKeyNames="ProductID" in the Gridview, you can configure it to automatically call the Delete method of the datasource, passing the primary key as the parameter.

So, your GridView may look something like this:
Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
   DataKeyNames="ProductID" DataSourceID="SqlDataSource1" AutoGenerateDeleteButton="True">
</asp:GridView>
and your datasource would look like this:
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
   ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
   SelectCommand="SELECT [ProductName], [QuantityPerUnit], [ProductID], [UnitPrice] FROM [Products]" 
   DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"           
      <DeleteParameters>
         <asp:Parameter Name="ProductID" Type="Int32" />
      </DeleteParameters>          
</asp:SqlDataSource>
This will automatically create the deletebutton, which will automatically call the delete method in the datasource when clicked.

_________________________________

Visit my blog at http://leedumond.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using command buttons in Access kelljw Access VBA 10 July 5th, 2006 12:42 PM
Using command buttons in Access kelljw BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 4th, 2006 07:21 AM
Labels as command buttons RichMW Access VBA 0 February 18th, 2005 09:10 AM
Graphics For Command Buttons Teqlump Access 1 November 24th, 2004 11:02 AM
Datagrid command buttons badgolfer ASP.NET 1.0 and 1.1 Basics 22 July 27th, 2004 02:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.