C
Chuck P
I have a GridView and I want to add a Column that has Edit save Cancel Button
in it.
I created the ITemplate column and the button gets created.
However, I can't get the grid to go into Edit mode.
I noticed that the GridView1_RowCommand does not fire.
I do this in the page load because the column seems to stay on a post back,
but the controls do not.
page load
if (m_GridView.Columns[m_GridView.Columns.Count - 1].HeaderText
== "Action")
m_GridView.Columns.RemoveAt(m_GridView.Columns.Count - 1);
TemplateField tf = new TemplateField();
tf.HeaderText = "Action";
// create a Template Field for the Edit Column
tf.ItemTemplate =
new GridViewInsertRowTemplate(TemplateType.ItemTemplate,
allowEdit, allowDelete);
tf.EditItemTemplate =
new GridViewInsertRowTemplate(TemplateType.EditItemTemplate,
allowEdit, allowDelete);
m_GridView.Columns.Add(tf);
public class GridViewInsertRowTemplate : ITemplate
{
private TemplateType m_templateType;
private bool m_AllowUpdate;
private bool m_AllowDelete;
public GridViewInsertRowTemplate(TemplateType templateType, bool
allowUpdate, bool allowDelete)
{
m_templateType = templateType;
m_AllowUpdate = allowUpdate;
m_AllowDelete = allowDelete;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (m_templateType)
{
case TemplateType.ItemTemplate:
if (m_AllowUpdate)
{
Button btnUpdate = new Button();
btnUpdate.ID = "btnUpdate";
btnUpdate.Text = "Update";
btnUpdate.CommandName = "Edit";
btnUpdate.CausesValidation = false;
container.Controls.Add(btnUpdate);
}
if (m_AllowDelete)
{
Button btnDelete = new Button();
btnDelete.ID = "btnDelete";
btnDelete.Text = "Delete";
btnDelete.CommandName = "Delete";
btnDelete.CausesValidation = false;
btnDelete.OnClientClick = "return
window.confirm('Are you sure you want to delete specified discipline?');";
container.Controls.Add(btnDelete);
}
break;
case TemplateType.EditItemTemplate:
Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.CommandName = "Update";
Button btnCancel = new Button();
btnCancel.ID = "btnCancel";
btnCancel.Text = "Cancel";
btnCancel.CommandName = "Cancel";
btnCancel.CausesValidation = false;
container.Controls.Add(btnCancel);
container.Controls.Add(btnSave);
break;
default:
break;
}
}
}
in it.
I created the ITemplate column and the button gets created.
However, I can't get the grid to go into Edit mode.
I noticed that the GridView1_RowCommand does not fire.
I do this in the page load because the column seems to stay on a post back,
but the controls do not.
page load
if (m_GridView.Columns[m_GridView.Columns.Count - 1].HeaderText
== "Action")
m_GridView.Columns.RemoveAt(m_GridView.Columns.Count - 1);
TemplateField tf = new TemplateField();
tf.HeaderText = "Action";
// create a Template Field for the Edit Column
tf.ItemTemplate =
new GridViewInsertRowTemplate(TemplateType.ItemTemplate,
allowEdit, allowDelete);
tf.EditItemTemplate =
new GridViewInsertRowTemplate(TemplateType.EditItemTemplate,
allowEdit, allowDelete);
m_GridView.Columns.Add(tf);
public class GridViewInsertRowTemplate : ITemplate
{
private TemplateType m_templateType;
private bool m_AllowUpdate;
private bool m_AllowDelete;
public GridViewInsertRowTemplate(TemplateType templateType, bool
allowUpdate, bool allowDelete)
{
m_templateType = templateType;
m_AllowUpdate = allowUpdate;
m_AllowDelete = allowDelete;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (m_templateType)
{
case TemplateType.ItemTemplate:
if (m_AllowUpdate)
{
Button btnUpdate = new Button();
btnUpdate.ID = "btnUpdate";
btnUpdate.Text = "Update";
btnUpdate.CommandName = "Edit";
btnUpdate.CausesValidation = false;
container.Controls.Add(btnUpdate);
}
if (m_AllowDelete)
{
Button btnDelete = new Button();
btnDelete.ID = "btnDelete";
btnDelete.Text = "Delete";
btnDelete.CommandName = "Delete";
btnDelete.CausesValidation = false;
btnDelete.OnClientClick = "return
window.confirm('Are you sure you want to delete specified discipline?');";
container.Controls.Add(btnDelete);
}
break;
case TemplateType.EditItemTemplate:
Button btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.CommandName = "Update";
Button btnCancel = new Button();
btnCancel.ID = "btnCancel";
btnCancel.Text = "Cancel";
btnCancel.CommandName = "Cancel";
btnCancel.CausesValidation = false;
container.Controls.Add(btnCancel);
container.Controls.Add(btnSave);
break;
default:
break;
}
}
}