S
SEliel
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
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