I've made a Template for adding controls to a DetailsView I have from code behind.
private class开发者_如何学Python edgFooterTemplate : ITemplate
{
private Button btn_Edit;
public void InstantiateIn(Control container)
{
btn_Edit = new Button();
btn_Edit.CausesValidation = false;
btn_Edit.CommandName = "Edit";
btn_Edit.ID = "btn_Edit";
btn_Edit.Text = "Edit";
container.Controls.Add(btn_Edit);
}
}
My problem is that I want to add an event handler on the control but I can't access btn_Edit in the DetailsView I made from code-behind as well.
You could init your edit button e.g. in the template constructor and add an edit click event to the template:
private class edgFooterTemplate : ITemplate
{
private Button btn_Edit;
public edgFooterTemplate()
{
btn_Edit = new Button();
btn_Edit.CausesValidation = false;
btn_Edit.CommandName = "Edit";
btn_Edit.ID = "btn_Edit";
btn_Edit.Text = "Edit";
}
public event EventHandler EditClick
{
add { this.btn_Edit.Click += value; }
remove { this.btn_Edit.Click -= value; }
}
public void InstantiateIn(Control container)
{
if (container != null)
{
container.Controls.Add(btn_Edit);
}
}
}
and then use it from the code behind:
protected void Page_Init(object sender, EventArgs e)
{
var footerTemplate = new edgFooterTemplate();
footerTemplate.EditClick += new EventHandler(footerTemplate_EditClick);
viewItems.FooterTemplate = footerTemplate;
}
and, finally, the event handler:
protected void footerTemplate_EditClick(object sender, EventArgs e)
{
// some logic here
}
精彩评论