Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > GridView ITemplate Columns

Reply
Thread Tools

GridView ITemplate Columns

 
 
Chuck P
Guest
Posts: n/a
 
      04-16-2007
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.Cou nt - 1);

TemplateField tf = new TemplateField();
tf.HeaderText = "Action";

// create a Template Field for the Edit Column
tf.ItemTemplate =
new GridViewInsertRowTemplate(TemplateType.ItemTemplat e,
allowEdit, allowDelete);
tf.EditItemTemplate =
new GridViewInsertRowTemplate(TemplateType.EditItemTem plate,
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;
}

}

}
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-17-2007
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.ItemTemplat e,true, true);

tf1.EditItemTemplate = new
GridViewInsertRowTemplate(TemplateType.EditItemTem plate, 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/subscripti...ult.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/subscripti...t/default.aspx.

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



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







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView w/ ITemplate Columns - Can't find data on Postback John ASP .Net Web Controls 1 11-03-2008 11:37 AM
GridView w/ ITemplate Columns - Can't find data on Postback John ASP .Net 0 10-31-2008 12:42 PM
template columns and bound columns in gridview Vinki ASP .Net Web Controls 2 04-25-2007 04:22 PM
Align table columns with gridview columns Jacksm ASP .Net 2 11-21-2006 09:49 PM
Dynamically added Templated Columns (Implementing ITemplate) in DataGrid - How do I wire up events? David ASP .Net Datagrid Control 5 02-18-2004 08:28 AM



Advertisments