Button.Click not raised in Gridview's Template column

Discussion in 'ASP .Net' started by SEliel@gmail.com, May 30, 2007.

  1. Guest

    Hello everyone:

    I'm programming a custom GridView, adding column by column
    dynamically. Every column is a TemplateField, and I've made a class
    hierarchy for each template (TextColumnTemplate,
    DropDownListColumnTemplate, ButtonColumnTemplate), implementing
    ITemplate.

    The problem is in ButtonColumnTemplate. Here's is my code:

    public class GridViewButtonTemplate : GridViewTemplate
    {
    private string command;

    public string Command
    {
    set { this.command = value != null ? value : ""; }
    }

    public GridViewButtonTemplate(DataControlRowType type, string
    columnName, string command)
    {
    this.templateType = type;
    this.columnName = columnName;
    this.command = command;
    }

    public GridViewButtonTemplate(DataControlRowType type, string
    columnName, string command, EventHandler cmd)
    {
    this.templateType = type;
    this.columnName = columnName;
    this.command = command;
    this.cmd = cmd;
    }

    public override void InstantiateIn(Control container)
    {
    Button btn = new Button();
    switch (templateType)
    {
    case DataControlRowType.Header:
    Literal lc = new Literal();
    lc.Text = "<b>" + text + "</b>";
    container.Controls.Add(lc);
    break;
    case DataControlRowType.DataRow:
    btn.Text = this.command;
    btn.DataBinding += new EventHandler(this.DataBinding);
    btn.Click += new EventHandler(this.BtnClicked);
    btn.CausesValidation = false;
    btn.UseSubmitBehavior = true;
    container.Controls.Add(btn);
    break;
    }

    }

    protected override void DataBinding(object sender, EventArgs e)
    {
    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    string raw = DataBinder.Eval(gvr.DataItem,
    columnName).ToString();
    btn.CommandArgument = (raw != null && raw != "") ? raw : "";
    }

    public void BtnClicked(object sender, EventArgs e)
    {
    // Expected behavior for button's click event
    }

    }

    As you can see, I'm adding correctly the respective event to each
    event handler of the controls. The DataBinding is working well, and
    everything is displayed as i wanted, but when i click a button from de
    ButtonColumn, there's no responding from the server... Any ideas?

    (excuse me if my English is not so good, i'm from Mexico)

    Thanks in advice
    , May 30, 2007
    #1
    1. Advertising

  2. Andy Guest

    ASP.NET buttons actually have two change handlers: one for clicks, and
    the other for commands.

    Could it be that you have setup only the command handler and not the
    click handler for the button, and this is why you are not getting any
    responses?
    Andy, May 30, 2007
    #2
    1. Advertising

  3. Guest

    Yes, i know that. I've tried with EventHandler for Click event, and
    CommandEventHandler for Command event (setting up button's CommandName
    and CommandArguments properties in method InstantiateIn), and in both
    cases i have no response from the server when button is clicked.

    Perhaps is something about GridView events, but i've read that one of
    the major improvements of GridView over DataGrid is that one event is
    raised in the control, no in the cell containing controls...
    , May 30, 2007
    #3
  4. Guest

    I tried to say "Thanks in advance" in the first post, excuses :S.
    , May 30, 2007
    #4
  5. Guest

    Well, while i'm waiting an idea, i've was making other thing related
    to the gridview...

    Since all the columns are displayed in edit mode, there must be a
    button that take all the changes made to the data displayed in the
    gridview, and i discovered that all gridview's information (columns,
    rows, etc.) has gone.

    Of course the gridview's datasource/databind executes in !IsPostBack
    segment of the code... so i think that's the cause of column button is
    not raising the event, because it doesn't exists anymore...

    Any ideas?
    , May 30, 2007
    #5
    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. Purvi T
    Replies:
    0
    Views:
    831
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    980
  3. _nabuchodonozor

    GridView button column click

    _nabuchodonozor, Sep 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Forum
    Sep 21, 2007
  4. KJ
    Replies:
    3
    Views:
    397
  5. _nabuchodonozor

    GridView button column click

    _nabuchodonozor, Sep 20, 2007, in forum: ASP General
    Replies:
    1
    Views:
    167
    Bob Barrows [MVP]
    Sep 20, 2007
Loading...

Share This Page