Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Asp.Net Programming

Button.Click not raised in Gridview's Template column


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

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?

-----------------------------------------------Reply-----------------------------------------------

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...

-----------------------------------------------Reply-----------------------------------------------

I tried to say "Thanks in advance" in the first post, excuses :S.

-----------------------------------------------Reply-----------------------------------------------
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?

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc