By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,685 IT Pros & Developers. It's quick & easy.

Button.Click not raised in Gridview's Template column

P: n/a
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

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


protected override void DataBinding(object sender, EventArgs e)
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string raw = DataBinder.Eval(gvr.DataItem,
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 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
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

May 30 '07 #2

P: n/a
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 '07 #3

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

May 30 '07 #4

P: n/a
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 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.