GridView ITemplate Columns

Discussion in 'ASP .Net Web Controls' started by Chuck P, Apr 16, 2007.

  1. Chuck P

    Chuck P Guest

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

    }

    }
    Chuck P, Apr 16, 2007
    #1
    1. Advertising

  2. 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.
    Steven Cheng[MSFT], Apr 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacksm
    Replies:
    2
    Views:
    839
    Paul Chalekian
    Nov 21, 2006
  2. John
    Replies:
    0
    Views:
    439
  3. rodchar

    using ITemplate for my gridview help

    rodchar, Apr 28, 2010, in forum: ASP .Net
    Replies:
    2
    Views:
    1,120
    rodchar
    Apr 28, 2010
  4. David
    Replies:
    5
    Views:
    262
    Jeffrey Tan[MSFT]
    Feb 18, 2004
  5. John
    Replies:
    1
    Views:
    168
    myles
    Nov 3, 2008
Loading...

Share This Page