L
lauralucas
Hello
I use a datagrid with databound columns and a "Edit, Update, Cancel"
button column. I tried this with column created in design time:
When I click the Edit 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'm using Visual Studio 2003 and asp.net 1.1
I have seen this question posted several times (I even copy pasted the
question from an already closed thread. why close threads?) but no
answers.
I'm very frustrated and angry.
In other thread, someone answered this: " 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... "
HOW??? where is the init event handler???
well, I already tried several workarounds to this, but the behavior
gets weirder and weirder.
What I need to know, is: how to assign an event handler to a control
hat was created dynamically.
After the MSDN example FAILED TO WORK, I tried this:
Now, I'm creating the button columns dynamically. Everything works fine
except when in edit mode.
on page_load:
--------------------------------------------------------------------------------------
ButtonColumn bcEdit;
ButtonColumn bcDelete;
ButtonColumn bcUpdate;
ButtonColumn bcCancel;
bcEdit = new ButtonColumn();
bcEdit.Text = "Edit";
bcEdit.CommandName = "EditEntry";
EntryDataGrid.Columns.Add(bcEdit);
bcDelete = new ButtonColumn();
bcDelete.Text = "Delete";
bcDelete.CommandName = "DeleteEntry";
EntryDataGrid.Columns.Add(bcDelete);
bcUpdate = new ButtonColumn();
bcUpdate.Text = "Update";
bcUpdate.CommandName = "UpdateEntry";
EntryDataGrid.Columns.Add(bcUpdate);
bcCancel = new ButtonColumn();
bcCancel.Text = "Cancel";
bcCancel.CommandName = "CancelEntry";
EntryDataGrid.Columns.Add(bcCancel);
doBindData();
------------------------------------------------------------------------------------------
in doBindData() I'm doing this:
this.sqlConnection1.Open();
this.sqlCommand1.CommandText = getSqlQuery();
this.EntryDataGrid.DataSource =
sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
this.EntryDataGrid.DataBind();
---------------------------------------------------------------------------------------------
then, I catch OnItemCommand like this:
private void EntryDataGrid_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// stop editing
EntryDataGrid.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
this.Label1.Text ="inserting";
//doBindData();
break;
case "UpdateEntry":
this.Label1.Text ="updating";
//doBindData();
break;
case "CancelEntry":
this.Label1.Text ="canceling";
//doBindData();
break;
case "EditEntry":
this.Label1.Text ="editing";
EntryDataGrid.EditItemIndex = e.Item.ItemIndex;
doBindData();
break;
}
}
first: if I dont do a doBindData on page load when Page.IsPostBack is
true, nothing works, no event gets called at all. So, I have to
doBindData twice when clicking edit, once in page_load and once in
EntryDataGrid_ItemCommand.
notice I have all four buttons already created all the time.
after edit mode is on, all buttons, when clicked, have e.CommandName ==
"CancelEntry", including edit button (that is still visible. remember i
created the 4 buttons dynamically).
I noticed the code being executed for every button changes: when not in
edit mode it reads:
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl0','') for edit
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl1','') for delete
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl2','') for update
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl3','') for cancel
but when the row is in edit mode, this changes to:
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl29','') for edit
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl30','') for delete
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl31','') for update
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl32','') for cancel
I don't know if that has anything to do.
JEEZ, I NEED THIS TO WORK, is there any working example of a datagrid
with this edit update cancel column?!?!?!?!
I use a datagrid with databound columns and a "Edit, Update, Cancel"
button column. I tried this with column created in design time:
When I click the Edit 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'm using Visual Studio 2003 and asp.net 1.1
I have seen this question posted several times (I even copy pasted the
question from an already closed thread. why close threads?) but no
answers.
I'm very frustrated and angry.
In other thread, someone answered this: " 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... "
HOW??? where is the init event handler???
well, I already tried several workarounds to this, but the behavior
gets weirder and weirder.
What I need to know, is: how to assign an event handler to a control
hat was created dynamically.
After the MSDN example FAILED TO WORK, I tried this:
Now, I'm creating the button columns dynamically. Everything works fine
except when in edit mode.
on page_load:
--------------------------------------------------------------------------------------
ButtonColumn bcEdit;
ButtonColumn bcDelete;
ButtonColumn bcUpdate;
ButtonColumn bcCancel;
bcEdit = new ButtonColumn();
bcEdit.Text = "Edit";
bcEdit.CommandName = "EditEntry";
EntryDataGrid.Columns.Add(bcEdit);
bcDelete = new ButtonColumn();
bcDelete.Text = "Delete";
bcDelete.CommandName = "DeleteEntry";
EntryDataGrid.Columns.Add(bcDelete);
bcUpdate = new ButtonColumn();
bcUpdate.Text = "Update";
bcUpdate.CommandName = "UpdateEntry";
EntryDataGrid.Columns.Add(bcUpdate);
bcCancel = new ButtonColumn();
bcCancel.Text = "Cancel";
bcCancel.CommandName = "CancelEntry";
EntryDataGrid.Columns.Add(bcCancel);
doBindData();
------------------------------------------------------------------------------------------
in doBindData() I'm doing this:
this.sqlConnection1.Open();
this.sqlCommand1.CommandText = getSqlQuery();
this.EntryDataGrid.DataSource =
sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
this.EntryDataGrid.DataBind();
---------------------------------------------------------------------------------------------
then, I catch OnItemCommand like this:
private void EntryDataGrid_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// stop editing
EntryDataGrid.EditItemIndex = -1;
switch (e.CommandName)
{
case "Insert":
this.Label1.Text ="inserting";
//doBindData();
break;
case "UpdateEntry":
this.Label1.Text ="updating";
//doBindData();
break;
case "CancelEntry":
this.Label1.Text ="canceling";
//doBindData();
break;
case "EditEntry":
this.Label1.Text ="editing";
EntryDataGrid.EditItemIndex = e.Item.ItemIndex;
doBindData();
break;
}
}
first: if I dont do a doBindData on page load when Page.IsPostBack is
true, nothing works, no event gets called at all. So, I have to
doBindData twice when clicking edit, once in page_load and once in
EntryDataGrid_ItemCommand.
notice I have all four buttons already created all the time.
after edit mode is on, all buttons, when clicked, have e.CommandName ==
"CancelEntry", including edit button (that is still visible. remember i
created the 4 buttons dynamically).
I noticed the code being executed for every button changes: when not in
edit mode it reads:
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl0','') for edit
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl1','') for delete
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl2','') for update
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl3','') for cancel
but when the row is in edit mode, this changes to:
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl29','') for edit
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl30','') for delete
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl31','') for update
javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl32','') for cancel
I don't know if that has anything to do.
JEEZ, I NEED THIS TO WORK, is there any working example of a datagrid
with this edit update cancel column?!?!?!?!