Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / February 2004

Tip: Looking for answers? Try searching our database.

Update and Cancel events fire as Edit

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 26 Feb 2004 06:55 GMT
I have a datagrid that is created at run time

  DataGrid dgG = new DataGrid();
  BoundColumn bcB;

  dgG.CellPadding = 5;
  dgG.CellSpacing = 0;
  dgG.GridLines = GridLines.Both;
  dgG.CssClass = "SectionTableLines";
  dgG.DataKeyField = "PlanWorkOrderID";
  dgG.ID = DataSetName + "." + Constants.datagrid + "." + "0";

  dgG.HeaderStyle.CssClass = "SectionHeader";

  bcB = new BoundColumn();

  bcB.DataField = "WorkOrder";
  bcB.HeaderText = "WorkOrder";
  dgG.Columns.Add(bcB);

  dgG.AutoGenerateColumns = false;

  EditCommandColumn eccE = new EditCommandColumn();
  eccE.EditText = "Edit";
  eccE.CancelText = "Cancel";
  eccE.UpdateText = "Update";
  eccE.HeaderText = "Edit";
  eccE.ButtonType = ButtonColumnType.LinkButton;
  eccE.ItemStyle.CssClass = "TxtBox";

  dgG.Columns.Add(eccE);
  dgG.EnableViewState =false;

  ButtonColumn btncBC = new ButtonColumn();
  btncBC.HeaderText = "Delete";
  btncBC.ButtonType = ButtonColumnType.LinkButton;
  btncBC.Text = "Delete";
  btncBC.CommandName = "Delete";

  dgG.Columns.Add(btncBC);

  dgG.DataSource = dtT;
  dgG.DataBind();

  dgG = qabQA.makeWorkOrder(dstForm1370.Tables[Constants.WorkOrder]);
  dgG.EditCommand  += new DataGridCommandEventHandler(dgG_EditCommand);
  dgG.DeleteCommand += new DataGridCommandEventHandler(dgG_DeleteCommand);
  dgG.UpdateCommand += new DataGridCommandEventHandler(dgG_UpdateCommand);
  dgG.ItemCommand  += new DataGridCommandEventHandler(dgG_ItemCommand);
  dgG.CancelCommand += new DataGridCommandEventHandler(dgG_CancelCommand);
  tcC.Controls.Add(dgG);  // tcC is a table cell

The event handlers are the standard stuff.

 private void dgG_EditCommand(object source, DataGridCommandEventArgs e)
 {
  Control ctlC;
  string sDGID;

  sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

  ctlC = FindControl(sDGID);

  ((DataGrid)ctlC).EditItemIndex = e.Item.ItemIndex;

  BindGrid();

 }

 private void dgG_DeleteCommand(object source, DataGridCommandEventArgs e)
 {
  DataTable dtT;
  int dgPK;

  dtT = dstForm1370.Tables[Constants.WorkOrder];
  dgPK = (int)e.Item.ItemIndex;
  dtT.Rows[dgPK].Delete();

  BindGrid();
 }

 private void dgG_ItemCommand(object source, DataGridCommandEventArgs e)
 {
  DataTable dtT;
  int dgPK;

  if (((LinkButton)e.CommandSource).CommandName == "Delete")
  {
   dtT = dstForm1370.Tables[Constants.WorkOrder];
   dgPK = (int)e.Item.ItemIndex;
   dtT.Rows[dgPK].Delete();

   BindGrid();
  }
  else
  {
  }
 }

 private void dgG_UpdateCommand(object source, DataGridCommandEventArgs e)
 {
  DataTable dtT;
  string sDGID;
  int iPK;

  dtT = dstForm1370.Tables[Constants.WorkOrder];

  sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

  iPK = (int)e.Item.ItemIndex;

  dtT.Rows[iPK]["WorkOrder"] = ((TextBox)(e.Item.FindControl(sDGID))).Text;

  BindGrid();
 }

 private void BindGrid()
 {
  DataTable dtT;
  Control ctlC;
  string sDGID;

  dtT = dstForm1370.Tables[Constants.WorkOrder];

  sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

  ctlC = FindControl(sDGID);

  ((DataGrid)ctlC).DataSource = dtT;
  ((DataGrid)ctlC).DataBind();
  Session["Form1370DataSet"] = dstForm1370.Copy();
 }

 private void dgG_CancelCommand(object source, DataGridCommandEventArgs e)
 {
  Control ctlC;
  string sDGID;

  sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

  ctlC = FindControl(sDGID);

  ((DataGrid)ctlC).EditItemIndex = -1;

  BindGrid();

 }

The Editcommand column renders properly on screen. When I click the Edit
Link button, it correctly fires the Edit event which calls my EditGrid
method and puts me in edit mode. So far, so good. But, when I click the
update button or the cancel button, it fires the Edit event again instead of
firing Update or Cancel events. In other words, no matter what button I
click, it always puts me in my EditGrid method. I don't know why.

I found one other person who had the exact same problem.  The advise was:

I think your problem may be in the page_load subroutine. Make sure you don't
bind any data to the at a grid there unless IsPostBack is false (the first
time you go there should be the only time data is bound to the grid). I
think when you hit the Update link, it goes to the page_load sub before it
does anything else. Thus, if you bind data there to the datagrid, you just
bind what you already have--not the changes you want to make.

I do not think that applies to me.  Here is my page_load event handler

 private void Page_Load(object sender, System.EventArgs e)
 {
  DataAccess objDA;

  if (Page.IsPostBack)
  {
   dstForm1370 = ((DataSet)Session["Form1370DataSet"]).Copy();
   BuildSections(); // data bind when grid created
  }
  else
  {
   objDA = new DataAccess();
   objDA.PlanID = Convert.ToInt32(Session["PlanID"]);
   dstForm1370 = objDA.GetDataSet("Form1370Data");

   BuildSections(); // data bind when grid created

   Session["Form1370DataSet"] = dstForm1370.Copy();
  }
 }

I am at a loss.  Anyone have any suggestions?

Thanks.

Steve
Steve - 26 Feb 2004 07:18 GMT
I just some some more articles.  Could it be that it has to do with the fact
that I have to recreate the entire form on each postback?

Steve
Scott Mitchell [MVP] - 26 Feb 2004 21:35 GMT
> I just some some more articles.  Could it be that it has to do with the fact
> that I have to recreate the entire form on each postback?

Steve, I've not looked at your problem closely, but this would be my
assumption as to the problem.  (It was my "gut feeling" when I skimmed
over your post earlier.)  Remember that you'll need to not only rebind the
controls on every load, but reassign the event handlers.  Too, you should
do this in the Page's Init event handler rather than in the Page_Load
event handler...

Signature

   Scott Mitchell
   mitchell@4guysfromrolla.com
   http://www.4GuysFromRolla.com
   http://www.ASPFAQs.com
   http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!

Steve - 26 Feb 2004 22:21 GMT
Thanks for looking at this.

I think I am already doing what you say.  I recreate the control, rebind it
and regenerate the event handlers in the module that creates the datagrid.
I don't think I can do this in the page's init event handler because I do
not know the control at that time.

Scott.  This is a critical function and I really need to get this working by
Friday.  Any other suggestions?

Steve

> > I just some some more articles.  Could it be that it has to do with the
> fact
[quoted text clipped - 6 lines]
> do this in the Page's Init event handler rather than in the Page_Load
> event handler...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.