Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with DataGrid.ItemCommand Method

Status
Not open for further replies.

James1981

Programmer
Nov 2, 2002
76
0
0
US
Hi,

I have a button in my datagrid, and an event handler for it which calls a function.. Only the eventhandler never calls the function, (or possibly the eventhandler never captures the button being pressed). Has any body had any similar problems or can anyone see any problems with my code:

Event Handler:
Code:
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);

My Method:
Code:
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{			
			string myID = e.Item.Cells[2].Text;
   
			if (e.CommandName == "cancel")
			{
                           // Method here		
			}
			DataGrid1.DataBind();
		}

My DataGrid

Code:
<asp:ButtonColumn Text=&quot;Cancel&quot; ButtonType=&quot;PushButton&quot; HeaderText=&quot;Cancel&quot; CommandName=&quot;cancel&quot;>
  <HeaderStyle HorizontalAlign=&quot;Center&quot;></HeaderStyle>
  <ItemStyle HorizontalAlign=&quot;Center&quot;></ItemStyle>
</asp:ButtonColumn>

Thanks

James
 
You've specified a cancelCommand, not an itemCommand.

Cancel is a reserved command that has it's own event handler:

this.DataGrid1.CancelCommand

If you want to handle it as a regular flavored command, then assign &quot;cancel&quot; to the commandArgument, not the commandName... or handle it as a cancelCommand.

Either way.

:)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Paul,

This turned out to be one issue, so thanks. However, another problem that was hidden; in the page load I was always rebinding the datagrid to the original data fetch. Instead I needed to implement a isPostBack test. This fixed this problem.

Cheers

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top