I have a Custom WebControl. Inside this control I add a button and I want it to access an EventHandler that is on the WebForm where the control is included. The handler handles with controls from the WebForm, so it has to be there. I could probably manage to take the button out of the control, but it would be better to keep it on the control, for organization sake.
public class LanguageSelection : WebControl
{
private List<Language> _Languages;
private CSSImageButton btnOk = new CSSImageButton();
private CSSImageButton btnClose = new CSSImageButton();
public List<Language> Languages
{
set { _Languages = value; }
get { if (_Languages != null) return _Languages; else; _Languages = LanguageManager.Select(); return _Languages; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Control parent;
Control container;
btnClose.CssClass = "sprReprove";
btnClose.DivClass = "float-right";
btnClose.OnClientClick = "$('#languagesOptions').hide('slow')";
btnOk.CssClass = "sprApprove";
btnOk.DivClass = "float-right";
btnOk.Click += new ImageClickEventHandler("btnSave_Click"); // this method here is on the webform where i included the control
// Get a reference to the ScriptManager object for the page
// if one exists.
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (sm == null || !sm.EnablePartialRendering)
{
// If partial rendering is not enabled, set the parent
// and container as a basic control.
container = new Control();
parent = container;
}
else
{
// If partial rendering is enabled, set the parent as
// a new UpdatePanel object and the container to the
// content template of the UpdatePanel object.
UpdatePanel up = new UpdatePanel();
container = up.ContentTemplateContainer;
parent = up;
}
container.Controls.Add(new LiteralControl("<div id=\"languagesOptions\" class=\"divSelectLanguages\">"));
container.Controls.Add(new LiteralControl(" <strong>Salvar conteúdo nestes idiomas?</strong>"));
container.Controls.Add(new LiteralControl("<table class=\"tblSelectLanguages\">"));
int i = 0;
foreach (Language l in Languages)
{
CheckBox cb = new CheckBox();
cb.Enabled = false;
if(i % 2 == 0) container.Controls.Add(new LiteralControl("</tr><tr>"));
container.Controls.Add(new LiteralControl("<td>"));
container.Controls.Add(cb);
container.Controls.Add(new LiteralControl(l.FullName));
container.Controls.Add(new LiteralControl("</td>"));
i++;
}
container.Controls.Add(new LiteralControl("</tr>"));
container.Controls.Add(new LiteralControl("</table>"));
container.Controls.Add(btnOk);
container.Controls.Add(btnClose);
container.Controls.Add(new LiteralControl("</div>"));
Controls.Add(parent);
}}