Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Problems with Datagrid Event


Message #1 by "Hugh McLaughlin" <hugh@k...> on Tue, 27 Aug 2002 15:03:33
Hello everyone and thanks for your hlep in advance.  I am working on an 
application that displays a list in a datagrid and then allows the user 
to click on an edit button to edit.  However, I cannot seem to get the 
edit event to fire properly.  the code for my datagrid is:

<ASP:DataGrid id="MyDataGrid" runat="server"		
	AutoGenerateColumns="False"
	CellPadding="5"
	GridLines="Horizontal"
	HeaderStyle-BackColor="silver"
	HeaderStyle-HorizontalAlign="center"
	FooterStyle-BackColor="silver"
	ShowFooter="True"			
	OnEditCommand="EditCommand">
	<Columns>
	<ASP:TemplateColumn HeaderText="Name">
		<ItemTemplate><%# Container.DataItem("szLastName") %
>&nbsp;<%# Container.DataItem("szFirstName") %></ItemTemplate>
	</ASP:TemplateColumn>
	<ASP:BoundColumn HeaderText="<b>First Name</b>" 
DataField="szUserRights" ItemStyle-HorizontalAlign="left"/>
	<ASP:TemplateColumn HeaderText="">
		<ItemTemplate>
			<ASP:Button CommandName="DeleteCommand" 
CommandArgument=<%# Container.DataItem("szID") %> Text="Delete" 
OnCommand="DeleteCommand" runat="server" />
		</ItemTemplate>
	</ASP:TemplateColumn>
        <asp:ButtonColumn 
         HeaderText="Edit" 
         ButtonType="PushButton" 
         Text="Edit" 
         CommandName="Edit" />
</Columns>
				
</ASP:DataGrid>

And the code for the edit command is:


	Sub EditCommand(Source As Object, E As DataGridCommandEventArgs)

		UserList.Visible=False
		'UserDetails.Visible=True

		TestLabel.Text="Edit"

		Dim UserID As Integer = (E.Item.ItemIndex)

	End Sub

I am not sure why this is not working.  any help would be greatly 
appreciated.  Thanks.
Message #2 by "Sampath, Ramanujam (Cognizant)" <SRamanuj@c...> on Tue, 27 Aug 2002 19:59:53 +0530
Well hugh.. try this way

<asp:datagrid id="grid1" runat="server" 
              OnEditCommand="EditRow"
              OnCancelCommand="CancelUpdate"
              OnUpdateCommand="UpdateRow"
              DataKeyField="au_id"
              AutoGenerateColumns="false" >

	<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" /> 	

	<template name="ItemTemplate">
		<asp:Label Text='<%# Container.DataItem("au_lname") %>'
runat="server"/>
	</template>
	<template name="EditItemTemplate">
		<asp:TextBox id="txtAuthor" Text='<%#
Container.DataItem("au_lname") %>' 
				runat="server"/>
	</template>
<asp:BoundColumn Headertext="ID" DataField="au_id" ReadOnly="true"/>
</asp:datagrid>

In the VB code window....

public sub page_load(sender as object,e as eventargs)

if page.ispostback=false then
	BindGrid()
end if

end sub

public sub BindGrid()
	cnn=new adoconnection("dsn=sample")
	cmd=new adodatasetcommand("select * from authors",cnn)
	cmd.filldataset(ds,"authors")
	grid1.datasource=ds.tables("authors").defaultview
	grid1.databind()
end sub

public sub EditRow(sender as Object, e as DataGridCommandEventArgs)
        grid1.EditItemIndex = e.Item.ItemIndex
        BindGrid()
end sub

public sub CancelUpdate(sender as Object, e as DataGridCommandEventArgs)
        grid1.EditItemIndex = -1
        BindGrid()
end sub

public sub DeleteRow(sender as Object,e as DataGridCommandEventArgs)
	'grid1.DataKeys.item(e.item.itemindex) will give value
	'of primary key column i.e. Au_ID
	'now construct and fire delete query
end sub

public sub UpdateRow(sender as Object,e as DataGridCommandEventArgs)
	dim txt1 as textbox
	dim txt2 as textbox
	
	txt1=e.Item.FindControl("txtAuthor")
	txt2=e.Item.FindControl("txtYear")
	
	'grid1.DataKeys.item(e.item.itemindex) will give value
	'of primary key column i.e. Au_ID
	'now get changed author as txt1.Text and year as txt2.text
	'construct and fire update query
End sub


Well what u exactly missed was u never created a Edit/Cancel/Delete column
of a datagrid.... and one more blunt mistake was u never binded the data
again by calling the DataGrid.DataBind .. because even after change the
datagrid is not bound and hence will loose the data... try this way and hope
this will help u a lot...

keep me postd if u have any issues....

==============================
S.Ramanujam
Programmer Analyst
Cognizant technology Solutions (p) Ltd. - CTS
38 & 39 Whites Road,
WCB - Whites Road Circular Building
Royapettah, Chennai - 600014
Ph : +xx xx xxx xxxx Extn 5113 [Off]
     : +xx xx xxx xxxx                 [Res]


-----Original Message-----
From: Hugh McLaughlin [mailto:hugh@k...]
Sent: Tuesday, August 27, 2002 8:34 PM
To: ASP+
Subject: [aspx] Problems with Datagrid Event


Hello everyone and thanks for your hlep in advance.  I am working on an 
application that displays a list in a datagrid and then allows the user 
to click on an edit button to edit.  However, I cannot seem to get the 
edit event to fire properly.  the code for my datagrid is:

<ASP:DataGrid id="MyDataGrid" runat="server"		
	AutoGenerateColumns="False"
	CellPadding="5"
	GridLines="Horizontal"
	HeaderStyle-BackColor="silver"
	HeaderStyle-HorizontalAlign="center"
	FooterStyle-BackColor="silver"
	ShowFooter="True"			
	OnEditCommand="EditCommand">
	<Columns>
	<ASP:TemplateColumn HeaderText="Name">
		<ItemTemplate><%# Container.DataItem("szLastName") %
>&nbsp;<%# Container.DataItem("szFirstName") %></ItemTemplate>
	</ASP:TemplateColumn>
	<ASP:BoundColumn HeaderText="<b>First Name</b>" 
DataField="szUserRights" ItemStyle-HorizontalAlign="left"/>
	<ASP:TemplateColumn HeaderText="">
		<ItemTemplate>
			<ASP:Button CommandName="DeleteCommand" 
CommandArgument=<%# Container.DataItem("szID") %> Text="Delete" 
OnCommand="DeleteCommand" runat="server" />
		</ItemTemplate>
	</ASP:TemplateColumn>
        <asp:ButtonColumn 
         HeaderText="Edit" 
         ButtonType="PushButton" 
         Text="Edit" 
         CommandName="Edit" />
</Columns>
				
</ASP:DataGrid>

And the code for the edit command is:


	Sub EditCommand(Source As Object, E As DataGridCommandEventArgs)

		UserList.Visible=False
		'UserDetails.Visible=True

		TestLabel.Text="Edit"

		Dim UserID As Integer = (E.Item.ItemIndex)

	End Sub

I am not sure why this is not working.  any help would be greatly 
appreciated.  Thanks.
---

ASP.NET 1.0 Namespace Reference with C#
http://www.wrox.com/acon11.asp?ISBN=1861007442

ASP.NET 1.0 Namespace Reference with VB.NET
http://www.wrox.com/acon11.asp?ISBN=1861007450

These books are a complete reference to the ASP.NET namespaces 
for developers who are already familiar with using ASP.NET. 
There is no trivial introductory material or useless .NET 
hype and the presentation of the namespaces, in an easy-to use 
alphabetical order ensures a user-friendly reference format.
We provide in-depth coverage of all the major ASP.NET classes, 
giving you those real-world tips that the documentation doesn't 
offer, and demonstrating complex techniques with simple 
examples.  

---

Message #3 by Imar Spaanjaars <Imar@S...> on Tue, 27 Aug 2002 23:01:55 +0200
Hi Hugh,

This might as well be a rather nasty bug, or a "by design" issue that 
hasn't been documented yet. Apparently, when you use

         ButtonType="PushButton"

in a datagrid, the OnItemCommand doesn't always fire. When you change the 
type to "LinkButton", the event does get raised and everything works as 
it's supposed to.
Funny thing is, I have an almost identical page that uses a PushButton as 
well, and it seems to work out well on that page.
Gotta do some more research, find out the differences and I'll post some 
info if I find out more.

Anybody else noticed this behavior? I've seen a few posts on other lists on 
this subject but never saw a satisfying answer to this problem (apart from: 
don't use the PushButton, use the LinkButton instead, or don't use the 
DataGrid like this and build your own solution)

Imar


At 03:03 PM 8/27/2002 +0000, you wrote:
>Hello everyone and thanks for your hlep in advance.  I am working on an
>application that displays a list in a datagrid and then allows the user
>to click on an edit button to edit.  However, I cannot seem to get the
>edit event to fire properly.  the code for my datagrid is:
>
><ASP:DataGrid id="MyDataGrid" runat="server"
>         AutoGenerateColumns="False"
>         CellPadding="5"
>         GridLines="Horizontal"
>         HeaderStyle-BackColor="silver"
>         HeaderStyle-HorizontalAlign="center"
>         FooterStyle-BackColor="silver"
>         ShowFooter="True"
>         OnEditCommand="EditCommand">
>         <Columns>
>         <ASP:TemplateColumn HeaderText="Name">
>                 <ItemTemplate><%# Container.DataItem("szLastName") %
> >&nbsp;<%# Container.DataItem("szFirstName") %></ItemTemplate>
>         </ASP:TemplateColumn>
>         <ASP:BoundColumn HeaderText="<b>First Name</b>"
>DataField="szUserRights" ItemStyle-HorizontalAlign="left"/>
>         <ASP:TemplateColumn HeaderText="">
>                 <ItemTemplate>
>                         <ASP:Button CommandName="DeleteCommand"
>CommandArgument=<%# Container.DataItem("szID") %> Text="Delete"
>OnCommand="DeleteCommand" runat="server" />
>                 </ItemTemplate>
>         </ASP:TemplateColumn>
>         <asp:ButtonColumn
>          HeaderText="Edit"
>          ButtonType="PushButton"
>          Text="Edit"
>          CommandName="Edit" />
></Columns>
>
></ASP:DataGrid>
>
>And the code for the edit command is:
>
>
>         Sub EditCommand(Source As Object, E As DataGridCommandEventArgs)
>
>                 UserList.Visible=False
>                 'UserDetails.Visible=True
>
>                 TestLabel.Text="Edit"
>
>                 Dim UserID As Integer = (E.Item.ItemIndex)
>
>         End Sub
>
>I am not sure why this is not working.  any help would be greatly
>appreciated.  Thanks.
>---
>
>ASP.NET 1.0 Namespace Reference with C#
>http://www.wrox.com/acon11.asp?ISBN=1861007442
>
>ASP.NET 1.0 Namespace Reference with VB.NET
>http://www.wrox.com/acon11.asp?ISBN=1861007450
>
>These books are a complete reference to the ASP.NET namespaces
>for developers who are already familiar with using ASP.NET.
>There is no trivial introductory material or useless .NET
>hype and the presentation of the namespaces, in an easy-to use
>alphabetical order ensures a user-friendly reference format.
>We provide in-depth coverage of all the major ASP.NET classes,
>giving you those real-world tips that the documentation doesn't
>offer, and demonstrating complex techniques with simple
>examples.
>
>---


Message #4 by "Hugh McLaughlin" <hugh@k...> on Wed, 28 Aug 2002 03:06:02
Hi Imar,

I had a hunch that this might be the case.  I had all of the code and 
more that was suggested in the previous post.  It seems like all of the 
examples are for linkbuttons with inline editing.  In my application, 
inline editing is not practical as it is a very large form that has to be 
populated.  I can get it to work using "OnCommand" within each button, 
but otherwise, it simply doesn't fire.  I would also be interested if 
anyone has encountered this or has a solution.

Thanks for your help and response.

> Hi Hugh,

This might as well be a rather nasty bug, or a "by design" issue that 
hasn't been documented yet. Apparently, when you use

         ButtonType="PushButton"

in a datagrid, the OnItemCommand doesn't always fire. When you change the 
type to "LinkButton", the event does get raised and everything works as 
it's supposed to.
Funny thing is, I have an almost identical page that uses a PushButton as 
well, and it seems to work out well on that page.
Gotta do some more research, find out the differences and I'll post some 
info if I find out more.

Anybody else noticed this behavior? I've seen a few posts on other lists 
on 
this subject but never saw a satisfying answer to this problem (apart 
from: 
don't use the PushButton, use the LinkButton instead, or don't use the 
DataGrid like this and build your own solution)

Imar


At 03:03 PM 8/27/2002 +0000, you wrote:
>Hello everyone and thanks for your hlep in advance.  I am working on an
>application that displays a list in a datagrid and then allows the user
>to click on an edit button to edit.  However, I cannot seem to get the
>edit event to fire properly.  the code for my datagrid is:
>
><ASP:DataGrid id="MyDataGrid" runat="server"
>         AutoGenerateColumns="False"
>         CellPadding="5"
>         GridLines="Horizontal"
>         HeaderStyle-BackColor="silver"
>         HeaderStyle-HorizontalAlign="center"
>         FooterStyle-BackColor="silver"
>         ShowFooter="True"
>         OnEditCommand="EditCommand">
>         <Columns>
>         <ASP:TemplateColumn HeaderText="Name">
>                 <ItemTemplate><%# Container.DataItem("szLastName") %
> >&nbsp;<%# Container.DataItem("szFirstName") %></ItemTemplate>
>         </ASP:TemplateColumn>
>         <ASP:BoundColumn HeaderText="<b>First Name</b>"
>DataField="szUserRights" ItemStyle-HorizontalAlign="left"/>
>         <ASP:TemplateColumn HeaderText="">
>                 <ItemTemplate>
>                         <ASP:Button CommandName="DeleteCommand"
>CommandArgument=<%# Container.DataItem("szID") %> Text="Delete"
>OnCommand="DeleteCommand" runat="server" />
>                 </ItemTemplate>
>         </ASP:TemplateColumn>
>         <asp:ButtonColumn
>          HeaderText="Edit"
>          ButtonType="PushButton"
>          Text="Edit"
>          CommandName="Edit" />
></Columns>
>
></ASP:DataGrid>
>
>And the code for the edit command is:
>
>
>         Sub EditCommand(Source As Object, E As DataGridCommandEventArgs)
>
>                 UserList.Visible=False
>                 'UserDetails.Visible=True
>
>                 TestLabel.Text="Edit"
>
>                 Dim UserID As Integer = (E.Item.ItemIndex)
>
>         End Sub
>
>I am not sure why this is not working.  any help would be greatly
>appreciated.  Thanks.
>---
>
>ASP.NET 1.0 Namespace Reference with C#
>http://www.wrox.com/acon11.asp?ISBN=1861007442
>
>ASP.NET 1.0 Namespace Reference with VB.NET
>http://www.wrox.com/acon11.asp?ISBN=1861007450
>
>These books are a complete reference to the ASP.NET namespaces
>for developers who are already familiar with using ASP.NET.
>There is no trivial introductory material or useless .NET
>hype and the presentation of the namespaces, in an easy-to use
>alphabetical order ensures a user-friendly reference format.
>We provide in-depth coverage of all the major ASP.NET classes,
>giving you those real-world tips that the documentation doesn't
>offer, and demonstrating complex techniques with simple
>examples.
>
>---



  Return to Index