Help with ASP.NET Datagrid

  • Thread starter Thread starter Fortra
  • Start date Start date
F

Fortra

I'm having trouble with the UpdateCommand event with the
DataGrid control. The event is wired up in the
InitializeComponents properly,

this.sitesDataGrid.UpdateCommand +=
new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.sitesDataGrid_UpdateCommand);

but when the "Update" button is clicked, the eventhandler
is never fired. The "Edit" command fires its
eventhandler, but "Update" and "Cancel" do not.

One peculiar thing I have noticed is that the ItemCommand
event fires for both the Edit and Update button. However,
for both, the CommandName (e.CommandName) is always "Edit".

What am I doing wrong???

Thanks in advance!!
 
Hi Fortra,

Here is the code I'm using for this.

I''m using asp:TemplateColumn to define my columns.

This is how I declare the grid in the ASP.NET page:
<asp:datagrid id=recordgrid runat="server" ShowHeader="false"
OnPageIndexChanged="Grid_Change" PagerStyle-Visible="False"
GridLines=Horizontal BorderStyle="none" BackColor="white"
autoGenerateColumns="False" DataSource="<%# GetDataGridSource()%>"
showfooter="False" visible="true" OnEditCommand="RecordEditCommand"
OnDeleteCommand="RecordDeleteCommand" Width="100%" CellPadding="0">

As you can see I declare the OnEditCommand, OnDelete directly in the page,
this avoid using InitializeComponents for this and is a cleaner solution
IMO.

The "Action" column is declared as:

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="25"
ItemStyle-HorizontalAlign="Center" >
<itemtemplate>
<asp:imagebutton ToolTip="Edit Record" runat="server"
Visible="true" CommandName="Edit" ImageUrl="images/ico_edit.gif"
ID="Imagebutton2" />
</itemtemplate>
</asp:templatecolumn>

As you see I'm using the CommandName property of the button, this indicate
what is the event to be risen on the datagrid.

As I said this come from a working page so you should have no problem using
it.

Hope this help,
 
Thanks again!

I did find my problem in the code-behind class - in the
InitializeComponents() method, I was calling the datagrid's
DataBind() method, event though I defined a BindData()
method to handle this that was called in the Page_Load()
method based on the IsPostBack property. As soon as I
removed this extra call, all of my datagrid events wired
up properly.
 
Hi,

Yes, of course I use it, it's there where the RecordEditCommand method is
declared , what I declare in the aspx code is which method in the code
behind will handle the event. I think this easier that use hook the event in
the InitializeComponents method.

Hope this help,
 
Back
Top