Control acessing event handler in web form class
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>&l开发者_StackOverflow社区t;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);
}}
Having your button handled by an event on the containing webform is not advisable. Ideally, your control should be completely self-contained. Instead, what you can do is have your button click event handled inside your control and then raise another event, which can be handled by the WebForm.
// This event will be handled by the webform
public event EventHandler OkButtonClicked;
protected void btnOk_Click(object sender, EventArgs e)
{
// Raise the okButtonClicked event
if (OkButtonClicked != null)
OkButtonClicked(sender, e);
}
// The btnOk button will be wired to our new event handler
btnOk.Click += new ImageClickEventHandler(btnOk_Click);
On your webform, you can have something like this:
<app:LanguageSelection ID="LanguageSelection1" OnOkButtonClicked="btnSave_Click" runat="server"/>
When the button is clicked inside the webcontrol, it would be handled by the btnOk_Click method inside the webcontrol. This would then raise the OkButtonClicked event which would be handled by the btnSave_Click method in WebForm containing the control.
精彩评论