Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / January 2005

Tip: Looking for answers? Try searching our database.

Changea  buttoncommand for one record only

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rock - 24 Jan 2005 17:49 GMT
I have a datagrid with the standard Edit and Delete columns that you can add
using the property builder.  I added a 3rd column for "Confirm Delete".

When the Delete button is clicked, it runs dg_ItemCommand, and changes the
visible column to say "Confirm Delete".  If user clicks on ConfirmDelete, it
then runs the handler dg_DeleteCommand and deletes the record.

The problem is that the column changes for every record so that every record
displays "Confirm Delete", and I'd rather display it only on the record the
user has clicked on.

Does anyone know how to do this?  My code is below.

Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand

       If (e.CommandName = "ConfirmDelete") Then
           dg.Columns(1).Visible = False
           dg.Columns(2).Visible = True
       End If

end sub

Private Sub dg_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.DeleteCommand

       Dim strGuid As String
       strGuid = e.Item.Cells(1).Text
       Call DeleteRecord(strGuid, strTableName)
       dg.Columns(1).Visible = True
       dg.Columns(2).Visible = False

End Sub

<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete"
CommandName="ConfirmDelete"></asp:ButtonColumn>
<asp:ButtonColumn Text="Confirm Delete" HeaderText="Confirm Delete"
CommandName="Delete" Visible=false></asp:ButtonColumn>
<asp:ButtonColumn Text="Copy" CommandName="Copy"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Yr" HeaderText="Year"></asp:BoundColumn>
Mike Ryan - 24 Jan 2005 21:49 GMT
Hello,

I don't think you'll get the results you're looking for with this
approach--setting the visibility would only work if you could hide rows
selectively and somehow merge the columns visually.

Here's a solution: keep what you have below, but delete the "Delete"
column. Then change your ItemCommand event to the following (hope this
works, haven't done VB in a while):

  If (e.CommandName = "ConfirmDelete") Then
    Dim lbtn As LinkButton
    lbtn = CType(e.Item.Cells(1).Controls(0), LinkButton)
    lbtn.Text = "Confirm Delete"
    lbtn.CommandName = "Delete"
  End If

What happens is the first time the link is clicked, the ItemCommand
event fires and changes the command from ConfirmDelete to Delete for
that link only. Clicking the link again then fires your Delete event and
you can do your bidness.

So don't create two columns--instead use one and change the function of
the selected link (in your Delete function you probably want to change
the CommandName back to "ConfirmDelete").

- Mike

> I have a datagrid with the standard Edit and Delete columns that you can add
> using the property builder.  I added a 3rd column for "Confirm Delete".
[quoted text clipped - 42 lines]
> ReadOnly="True"></asp:BoundColumn>
> <asp:BoundColumn DataField="Yr" HeaderText="Year"></asp:BoundColumn>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.