GridView ITemplate Columns

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;
}

}

}
 
S

Steven Cheng[MSFT]

Hi Chuck,

As for the custom TemplateField's Item/EditTemplate, I have performed some
test on myside, I think the Template class should be correct since it works
correctly in my test page. Maybe the problem is due to how you create and
add the custom template or the TemplateField in to GridView.Columns
collection? here is my test page's column creation codelogic, I put it in
Page_Init event, you can also have a test to see whether it works:

==================
protected void Page_Init(object sender, EventArgs e)
{
TemplateField tf1 = new TemplateField();
tf1.HeaderText = "My template field";

tf1.ItemTemplate = new
GridViewInsertRowTemplate(TemplateType.ItemTemplate,true, true);

tf1.EditItemTemplate = new
GridViewInsertRowTemplate(TemplateType.EditItemTemplate, true, true);

GridView1.Columns.Add(tf1);

}
======================

If there is any other finding, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top