 |
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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
|
|
|

July 24th, 2004, 12:25 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Edit/Delete ButtonColumn not working
I have a datagrid. I wish to have 2 buttons on each row for editing and deleting items. I'm not using "in-place" editing, but rather a more elaborate set of inputs that exist on the page after the datagrid, so I only need an edit button.
The datagrid's columns node contains:
<asp:buttoncolumn text="Edit" commandname="Edit" buttontype="PushButton" />
<asp:buttoncolumn text="Delete" commandname="Delete" buttontype="PushButton" />
According to the documentation, specifying the commandname as "Edit" or "Delete" will fire off the datagrid's built in events "EditCommand" and "DeleteCommand", respectively (as well as the "ItemCommand" event). However, I can't get any of those events to fire. I can work around the edit button problem with a regular EditCommandColumn but the delete is still an issue.
I have installed a technology preview build of .NET 2.0 and VS.NET 2005 so I thought that this might be some strange problem with having that on here. However, I put the application on a machine that only has framework 1.1 and this problem remained.
Has anyone experienced this? I'm sure I've done something very similar in the past and had it work but I am now perplexed.
|

July 24th, 2004, 01:47 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
On further investigation I have found that the buttons seem to work when I use them in LinkButton form but not in PushButton form. Very strange indeed. Also, I tried using an EditCommandColumn, but that didn't work either (except when using LinkButtons).
|

July 25th, 2004, 01:17 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Push button & LinkButton can not be a good reasen.
Hope this sample could help u.
Code:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
void Page_Load( Object s, EventArgs e )
{
if ( !IsPostBack )
BindData();
}
void BindData()
{
SqlConnection myConnection;
SqlCommand myCommand;
myConnection = new SqlConnection( "Server=Localhost;uid=sa;Database=Pubs" );
myCommand = new SqlCommand( "Select au_id, au_lname, au_fname, phone from Authors order by au_lname", myConnection );
myConnection.Open();
myDataList.DataSource = myCommand.ExecuteReader();
myDataList.DataBind();
myConnection.Close();
}
void editAuthor( Object s, DataListCommandEventArgs e )
{
myDataList.EditItemIndex = e.Item.ItemIndex;
BindData();
}
void cancelEdit( Object s, DataListCommandEventArgs e )
{
myDataList.EditItemIndex = -1;
BindData();
}
void deleteAuthor( Object s, DataListCommandEventArgs e )
{
SqlConnection myConnection;
SqlCommand myCommand;
String sqlString;
myConnection = new SqlConnection( "Server=Localhost;uid=sa;Database=Pubs" );
sqlString = "Delete Authors Where au_id=@authorID";
myCommand = new SqlCommand( sqlString, myConnection );
myCommand.Parameters.Add( new SqlParameter( "@authorID", SqlDbType.VarChar, 11 ) );
myCommand.Parameters[ "@authorID" ].Value = myDataList.DataKeys[ e.Item.ItemIndex ];
myConnection.Open();
myCommand.ExecuteNonQuery();
myDataList.DataBind();
myConnection.Close();
myDataList.EditItemIndex = -1;
BindData();
}
void updateAuthor( Object s, DataListCommandEventArgs e )
{
SqlConnection myConnection;
SqlCommand myCommand;
String sqlString;
myConnection = new SqlConnection( "Server=Localhost;uid=sa;Database=Pubs" );
sqlString = "Update Authors Set au_lname=@lastname, au_fname=@firstname, phone=@phone Where au_id=@authorID";
myCommand = new SqlCommand( sqlString, myConnection );
myCommand.Parameters.Add( new SqlParameter( "@lastname", SqlDbType.VarChar, 40 ) );
myCommand.Parameters[ "@lastname" ].Value = ( (TextBox) e.Item.FindControl( "lastname" ) ).Text;
myCommand.Parameters.Add( new SqlParameter( "@firstname", SqlDbType.VarChar, 20 ) );
myCommand.Parameters[ "@firstname" ].Value = ( (TextBox) e.Item.FindControl( "firstname" ) ).Text;
myCommand.Parameters.Add( new SqlParameter( "@phone", SqlDbType.Char, 12 ) );
myCommand.Parameters[ "@phone" ].Value = ( (TextBox) e.Item.FindControl( "phone" ) ).Text;
myCommand.Parameters.Add( new SqlParameter( "@authorID", SqlDbType.VarChar, 11 ) );
myCommand.Parameters[ "@authorID" ].Value = myDataList.DataKeys[ e.Item.ItemIndex ];
myConnection.Open();
myCommand.ExecuteNonQuery();
myDataList.DataBind();
myConnection.Close();
myDataList.EditItemIndex = -1;
BindData();
}
</Script>
<html>
<head><title>Edit Authors</title></head>
<body>
<form Runat="Server">
<asp:DataList id="myDataList" cellpadding=10 cellspacing=0 gridlines="both"
RepeatColumns="3" RepeatDirection="Horizontal" DataKeyField="au_id"
OnEditCommand="editAuthor" OnDeleteCommand="deleteAuthor"
OnUpdateCommand="updateAuthor" OnCancelCommand="cancelEdit" Runat="Server">
<ItemTemplate>
<asp:LinkButton Text="Edit" CommandName="edit" Runat="Server"/>
<%# DataBinder.Eval( Container.DataItem, "au_lname" )%>
</ItemTemplate>
<EditItemTemplate>
<b>Last Name:</b>
<br><asp:TextBox id="lastname"
text='<%# DataBinder.Eval( Container.DataItem, "au_lname" ) %>'
Runat="Server"/>
<p>
<b>First Name:</b>
<br><asp:TextBox id="firstname"
text='<%# DataBinder.Eval( Container.DataItem, "au_fname" ) %>'
Runat="Server"/>
<p>
<b>Phone:</b>
<br><asp:TextBox id="phone"
text='<%# DataBinder.Eval( Container.DataItem, "phone" ) %>'
Runat="Server"/>
<p>
<asp:Button Text="Update" CommandName="update" Runat="Server"/>
<asp:Button Text="Delete" CommandName="delete" Runat="Server"/>
<asp:Button Text="Cancel" CommandName="cancel" Runat="Server"/>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
Always:),
Hovik Melkomian.
|

July 25th, 2004, 07:45 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Peter I want to tell you evrything I have got from CommandName property
I think CommandName property in DataGrid behaves just like CommandName in a simple Button
I don't think it fires an event(like EditCommand and UpdateCommand)just we can use it for our perpose
for example we want to do some arithmetical instructions,... here we build four Buttons and give them four different
CommandName (like Addition,Subtraction,Division,multiplication)and then we write only one code then we could use
e.CommanName for determining which Button raised the event and then we do our different methods...
I think in a dataGrid only ItemCommand event fiers!!!(against the documentation!!!) and DataGridCommandEventArgs gives us our CommanName
we determined (and also CommanSource and CommanArgument)for determining which procedure should be executed
all the things I told were depend on my opinions...
I don't know realy what happends here and what is right or wrong!!!
--------------------------------------------
Mehdi
I'm waiting for your better idea.
|

July 25th, 2004, 08:42 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Melvik:
I'm working with a DataGrid. Your example uses a DataList. I'm not sure how that is to help me. I see that you are using asp:button controls in the list.
Mehdi:
The documentation states that there are several predefined keywords which can be used in the CommandName property of the ButtonColumn. These are: "Select", "Edit", "Update", "Cancel", "Delete", "Page", and "Sort" (I thinks that's all of them). When you use a ButtonColumn it is supposed to fire the ItemCommand event. If you use one of these predefined keywords it is supposed to fire off the respective event as well as the ItemCommand event. This is where I am having the problem. I can not get any of those events to fire (including ItemCommand).
My understanding is that when you use a ButtonColumn in a datagrid, the grid generats an asp:button control in the cell. This should result in behavior similar to if I had manually put an asp:button into a TemplateColumn. Maybe there is a problem with using the button column.
|

July 25th, 2004, 09:55 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Peter I confirm all the thing you told
>>When you use a ButtonColumn it is supposed to fire the ItemCommand event. If you use one of these predefined keywords it is >>supposed to fire off the respective event as well as the ItemCommand event
It seems such keywords don't work fine .I think here Bill Gates has forgotten to compile such keywords
in .NET :))
I think CommandName doesn't have any special keywords...I think it behaves just like a simple argument could be usefull
when event occurs
Peter anyway in evry situation ItemCommand event fires(through the child controls of the DataGrid
in evry type of columns like TemplateColumns,ButtonColumns,...)
but here just like you I can't get the rules of such keywords used within CommandName property
for rasing different events.
all the things I told were depend on my opinions...They could be wrong.
maybe I'm completly in a wrong direction...
Can anyone example me use of such keywords?
With Thanks.
--------------------------------------------
Mehdi
I'm waiting for your better idea.
|

July 26th, 2004, 10:54 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Sorry I didnt see that, but I guess there shouldnt be much difference! anyway sorry!
Always:),
Hovik Melkomian.
|

August 27th, 2004, 03:00 AM
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:Originally posted by planoie
<asp:buttoncolumn text="Edit" commandname="Edit" buttontype="PushButton" />
<asp:buttoncolumn text="Delete" commandname="Delete" buttontype="PushButton" />
|
change Edit to edit (like DataList)
HtH.
--------------------------------------------
Mehdi.:)
|

August 29th, 2004, 07:08 PM
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
not shure if this will help but you'll find a lot of dtg info on this page, i'm quit shure you'll find what you need.
http://samples.gotdotnet.com/quickst..._datagrid.aspx
Frank Vandeven
Belgium
|

August 30th, 2004, 07:39 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hmmm... Peter, I bet DataGridGirl can help...
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
how to EDIT,delete,add,items in my database |
zedekiah |
BOOK: Beginning ASP 3.0 |
2 |
May 15th, 2008 08:16 AM |
How to edit/delete list items through SPGridView |
meera |
SharePoint Development |
0 |
December 7th, 2007 11:32 AM |
save, edit, and delete |
sime_tyres |
VB Databases Basics |
1 |
September 1st, 2006 09:45 PM |
Edit/Add/Delete within a DataGrid |
ozzy |
VB.NET 2002/2003 Basics |
5 |
January 23rd, 2005 01:17 AM |
Edit or Delete records |
tsimsha |
Classic ASP Basics |
6 |
October 30th, 2004 03:26 AM |
|
 |