Re: Bubbling events on a server side control
Dean Cleaver <[email protected]> Thu, 3 Apr 2008 23:44:52 +1300
| Newsgroups | gmane.comp.windows.devel.dotnet.web |
|---|---|
| Message-ID | <4543A775711185429E59B18055D416742322E4@sxslakl004.xceptionsoftware.com> |
Brilliant! Thank you - after spending the past 10 hours scratching my head, I just couldn't see it! Dino -----Original Message----- From: Discussion of building .NET applications targeted for the Web [mailto:[email protected]] On Behalf Of Efran Cobisi Sent: Thursday, 3 April 2008 23:02 To: [email protected] Subject: Re: [DOTNET-WEB] Bubbling events on a server side control Adding the handler to your DropDownList's SelectedIndexChanged event in the Render phase should bee too late for the framework to take it into account properly. Add it while being in the Init phase instead, just before adding the DropDownList to the Controls collection; that should make the trick. protected override void OnInit(EventArgs e) { Controls.Clear(); // Setup children's events handlers this.DropDownList.SelectedIndexChanged +=3D DropDownList_SelectedIndexChanged; Controls.Add(this.TextBox); Controls.Add(this.DropDownList); } HTH -- Efran Cobisi http://www.cobisi.com Dean Cleaver wrote: > I've tried to create a control that can be either a TextBox or a > DropDownList, but am having trouble "bubbling" the SelectedIndexChanged > event for the DropDownList (within an UpdatePanel if that makes any > difference). My code is like this (snipped a bit for brevity) - is there > something obvious I am missing? > > Dino > > > > using System; > using System.ComponentModel; > using System.Data; > using System.Security.Permissions; > using System.Web; > using System.Web.UI; > using System.Web.UI.WebControls; > using AjaxControlToolkit; > > namespace Client.Project.WebControls > { > [ > DefaultEvent("SelectedIndexChanged"), > DefaultProperty("Text"), > ToolboxData("<{0}:DetailInputBox runat=3D\"server\"> > </{0}:DetailInputBox>"), > ] > public class DetailInputBox : WebControl, IPostBackEventHandler > { > protected override void OnInit(EventArgs e) > { > Controls.Clear(); > > Controls.Add(this.TextBox); > Controls.Add(this.DropDownList); > } > > protected override void Render(HtmlTextWriter writer) > { > if (this.IsValid || this.ReadOnly) > writer.WriteLine("<div class=3D\"detailBlock\">"); > else > writer.WriteLine("<div = class=3D\"detailBlockError\">"); > > writer.WriteLine(" <div class=3D\"detailInput\">"); > > if (this.DropDownList.Items.Count > 0) > { > this.DropDownList.SelectedIndexChanged +=3D > DropDownList_SelectedIndexChanged; > this.DropDownList.RenderControl(writer); > } > else > this.TextBox.RenderControl(writer); > > writer.WriteLine(" </div>"); > writer.WriteLine(" <div class=3D\"clear\"></div>"); > writer.WriteLine("</div>"); > } > > [Browsable(false),] > private TextBox textBox =3D null; > /// <summary> > /// Gets or sets the required text box validator. > /// </summary> > /// <value>The required text box validator.</value> > private TextBox TextBox > { > get > { > if (textBox =3D=3D null) > { > textBox =3D new TextBox(); > textBox.ID =3D this.ID + "Text"; > } > return textBox; > } > } > > [Browsable(false),] > private DropDownList dropDownList =3D null; > /// <summary> > /// Gets or sets the required text box validator. > /// </summary> > /// <value>The required text box validator.</value> > public DropDownList DropDownList > { > get > { > if (dropDownList =3D=3D null) > { > dropDownList =3D new DropDownList(); > dropDownList.ID =3D this.ID + "List"; > } > return dropDownList; > } > } > > private bool isValid =3D true; > [Browsable(false)] > public bool IsValid > { > get { return this.isValid; } > set { this.isValid =3D value; } > } > > [ > Description("The Details Fields ValidationGroup"), > Category("Custom Properties"), > ] > public string ValidationGroup > { > get { return this.TextBox.ValidationGroup; } > set > { > this.TextBox.ValidationGroup =3D value; > this.DropDownList.ValidationGroup =3D value; > } > } > > public ListItemCollection Items > { > get { return this.DropDownList.Items; } > } > > public string SelectedValue > { > get { return this.DropDownList.SelectedValue; } > } > > public int SelectedIndex > { > get { return this.DropDownList.SelectedIndex; } > set { this.DropDownList.SelectedIndex =3D value; } > } > > void DropDownList_SelectedIndexChanged(object sender, EventArgs > e) > { > this.OnSelectedIndexChanged(e); > } > > public event EventHandler SelectedIndexChanged; > > protected virtual void OnSelectedIndexChanged(EventArgs args) > { > EventHandler thisEvent =3D this.SelectedIndexChanged; > > if (thisEvent !=3D null) > thisEvent(this, args); > } > > public bool AutoPostBack > { > get { return this.DropDownList.AutoPostBack; } > set > { > this.TextBox.AutoPostBack =3D value; > this.DropDownList.AutoPostBack =3D value; > } > } > > public void RaisePostBackEvent(string eventArgument) > { > this.OnSelectedIndexChanged(EventArgs.Empty); > } > } > } > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This list is hosted by DevelopMentor=AE http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com