PC Review


Reply
Thread Tools Rate Thread

Button.Click not raised in Gridview's Template column

 
 
SEliel@gmail.com
Guest
Posts: n/a
 
      30th May 2007
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

 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      30th May 2007
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 With Quote
 
SEliel@gmail.com
Guest
Posts: n/a
 
      30th May 2007
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 With Quote
 
SEliel@gmail.com
Guest
Posts: n/a
 
      30th May 2007
I tried to say "Thanks in advance" in the first post, excuses :S.

 
Reply With Quote
 
SEliel@gmail.com
Guest
Posts: n/a
 
      30th May 2007
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 button column click _nabuchodonozor Microsoft ASP .NET 1 21st Sep 2007 09:29 PM
Button Inside Header template of Gridview--gives error on click divya Microsoft ASP .NET 0 9th Aug 2007 12:04 PM
Click event not raised when clicking a button after displaying a dialog box Natalya.Pinsker@constellation.com Microsoft Dot NET Framework Forms 2 5th Dec 2006 06:16 PM
Click Events in programmatically created GridView Template =?Utf-8?B?anRlbmNhdGU=?= Microsoft ASP .NET 0 28th Jul 2006 09:35 PM
Please help : Click event not raised when clicking button!!! Pascal Microsoft Dot NET Framework Forms 4 24th Mar 2004 08:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:32 PM.