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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with EditItemTemplate in DataGrid 2

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
I have a column in a datagrid that displays a checkbox when its not in edit mode. It edit mode it displays Accept and Cancel links. When the user clicks the Accept link it should trigger the OnUpdateCommand. But when I step through the code-behind it simply executes the Page_Load method and exits. It never gets to the Update method.


This is where I declare the methods to call for the different commands:
Code:
<asp:DataGrid id=&quot;dataGridTopLevelMenu&quot; runat=&quot;server&quot; HorizontalAlign=&quot;Center&quot; CellPadding=&quot;3&quot; CellSpacing=&quot;1&quot; WIDTH=&quot;100%&quot; BorderWidth=&quot;0&quot; BorderColor=&quot;&quot; AutoGenerateColumns=&quot;false&quot; GridLines=&quot;Both&quot; OnEditCommand=&quot;dataGridTopLevelMenu_Edit&quot; OnCancelCommand=&quot;dataGridTopLevelMenu_Cancel&quot; OnUpdateCommand=&quot;dataGridTopLevelMenu_Update&quot; OnItemCommand=&quot;dataGridTopLevelMenu_ItemCommand&quot;>

This is my templateColumn:
Code:
<asp:TemplateColumn ItemStyle-CssClass=&quot;style&quot; ItemStyle-Wrap=&quot;False&quot; HeaderStyle-CssClass=&quot;td_Style_01&quot; HeaderText=&quot;X&quot;>
						<ItemTemplate>
							<asp:CheckBox runat=&quot;server&quot; ID=&quot;chkDelete&quot; checked='<%#DataBinder.Eval (Container.DataItem, &quot;delete&quot;)%>'>
							</asp:CheckBox>
						</ItemTemplate>
						<EditItemTemplate>
							<asp:LinkButton CommandName=&quot;Update&quot; Text=&quot;Accept&quot; Runat=&quot;server&quot; ID=&quot;lnkUpdate&quot; />
							<BR />
							<asp:LinkButton CommandName=&quot;Cancel&quot; Text=&quot;Cancel&quot; CausesValidation=&quot;false&quot; Runat=&quot;server&quot; ID=&quot;lnkCancel&quot; />
						</EditItemTemplate>
					</asp:TemplateColumn>

Please help me to figure out what I'm doing wrong. I've been having this problem for over a day now and I'm working on tight deadlines. Any suggestions or clues would be greatly appreciated.

Thanks in advance,
- VB Rookie
 
First, try the un-capitalization of your command names. If that doesn't work, then try adding a &quot;handles&quot; statement to the end of your routine declaration:

protected sub .... ) handles dataGridTopLevelMenu.updateCommand

If that doesn't work, then can we see the code for your subroutine here (or at least the declaration for it)?
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks so much man! I mapped the eventhandler to the grid in the oninit override and took it out of the aspx.

Here is the code:

Code:
override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent ();
			base.OnInit (e);

			// MAKE SURE THAT VIEW STATE IS ENABLED AND MAP THE EVENT TO THE HANDLER
			this.dataGridTopLevelMenu.ItemCommand += new DataGridCommandEventHandler (this.dataGridTopLevelMenu_ItemCommand);
		}

I made other changes as well that one of my fellow coders suggested and now smiles have once again encircled my face. Happy days are here again, the skies are ....

Well you get the picture.
Deadlines ... shmeadlines.
Thanks again Link ... this one had me stumped for a while.

- VB Rookie
 
I seem to have better luck wiring event handlers in code.

However, I have also recently seen C# handle this better, as well... especially with dynamic controls & events.

VB seems to be a little buggy in this area (ie. events taking two clicks to fire, events not firing at all, etc...)

Yet another nail in the coffin of VB for me (adding to the handling of NULL -- which is infinitely more elegant in C#, as well as its lack of overloaded operators...)
penny1.gif
penny1.gif

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

I tried handler and it fixed problem when IIS is hosted on WindowsXP pro. But the same code didn't work on Win2K server.

Similar simpthoms: Edit button expands to Update Cancel on the first hit and then if you hit Update it calls Edit again or Cancel - doesn't call handler at all. When debugging I found that DataGrid loses

this.masterDataGrid.EditItemIndex and
this.masterDataGrid.SelectedIndex and resets them to -1

So, my fix uses static int to remember this and then reset in Page_Load:

static int selectedIndex=-1; //this static helps me force DataGrid stay on the line, not to reset to -1


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
this.LoadDataSet();
this.masterDataGrid.EditItemIndex=selectedIndex;
this.masterDataGrid.SelectedIndex = selectedIndex;
this.masterDataGrid.DataBind();

}


and in Update, Cancel and Edit handlers:

protected void MasterDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
///findout the selected item index and bindgrid again
selectedIndex= (int)E.Item.ItemIndex;
this.masterDataGrid.SelectedIndex=(int)E.Item.ItemIndex;
this.masterDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
this.LoadDataSet();
this.masterDataGrid.DataBind();
}
protected void MasterDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
///findout the selected item index and bindgrid again
this.masterDataGrid.EditItemIndex = -1;
selectedIndex=-1;
this.LoadDataSet();
this.masterDataGrid.DataBind();
}
protected void MasterDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{
string EmployeeID = ((TextBox)E.Item.Cells[1].Controls[0]).Text;
string EmployeeLastName = ((TextBox)E.Item.Cells[2].Controls[0]).Text;
string EmployeeFirstName = ((TextBox)E.Item.Cells[3].Controls[0]).Text;
string EmployeeMiddleName = ((TextBox)E.Item.Cells[4].Controls[0]).Text;
System.Int16 EmployeeStatus =System.Int16.Parse(((TextBox)E.Item.Cells[5].Controls[0]).Text);

string SQLStatement=&quot;UPDATE JCEmployee &quot;+
&quot;SET EmployeeLastName='&quot;+EmployeeLastName+&quot;', &quot;+
&quot;EmployeeFirstName='&quot;+EmployeeFirstName+&quot;', &quot;+
&quot;EmployeeMiddleName ='&quot;+EmployeeMiddleName+&quot;', &quot;+
&quot;EmployeeStatus =&quot;+EmployeeStatus.ToString()+
&quot; WHERE EmployeeID='&quot;+EmployeeID+&quot;'&quot;;
System.Data.SqlClient.SqlConnection tmpConnection =new System.Data.SqlClient.SqlConnection(@&quot;server=andrew2;database=Adventure_Works_Cycle_MSCRM;uid =sa;pwd=;&quot;);

System.Data.SqlClient.SqlCommand tmpCommand = new System.Data.SqlClient.SqlCommand(SQLStatement,tmpConnection);


tmpCommand.CommandType=CommandType.Text;

tmpConnection.Open();
tmpCommand.ExecuteNonQuery();
tmpConnection.Close();


///findout the selected item index and bindgrid again

this.masterDataGrid.EditItemIndex = -1;
selectedIndex=-1;
this.LoadDataSet();
this.masterDataGrid.DataBind();
}

If somebody knows the real unswer, please post, in my case I don't like to use ugly fixes, but have to

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top