|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Composite Controls, eventhandlers and knowing which control caused postbackI have created a composite control that has a number of standard asp.net controls on it that can themselves cause postbacks. What i need to do in my composite control is to determine which consituent control caused a postback. for example a have a consituent controls with two buttons on it "button1" and "button2" I have registered my control for postbacks using "Page.RegisterRequiresPostBack(this);" I have include a dynamic handler for my buttons and registered this however when I click the button the code in my dynamic handler does not get raised. however, because I have implemented "IPostBackDataHandler " the event "RaisePostDataChangedEvent()" does get raised however this seems to have no information in it about which button was clicked all it tells me is that a postback occured. what ideally i need to do is get the dynamic button handlesr to work, can any body give me any advice on this or point me to a good article that shows how to run button handlers from within compsoite controls. i have included my code below for anybody's reference. -- this is my entire test user control. many thanks in advance. cheers martin. using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Collections.Specialized; namespace CompositeControls { /// <summary> /// Summary description for Compositecontrol. /// </summary> [DefaultProperty("Text"), ToolboxData("<{0}:Compositecontrol runat=server></{0}:Compositecontrol>")] public class Compositecontrol : System.Web.UI.WebControls.WebControl, IPostBackDataHandler { private Panel pnl; private string text; [Bindable(true), Category("Appearance"), DefaultValue("")] public string txtFirstName { get { return text; } set { text = value; } } protected override void OnInit(EventArgs e) { base.OnInit(e); if (Page != null) Page.RegisterRequiresPostBack(this); } protected override void CreateChildControls() { System.Web.HttpContext.Current.Trace.Write("CreateChildControls()"); pnl = new Panel(); TextBox txtbox = new TextBox(); txtbox.ID = UniqueID; txtbox.Text = txtFirstName; pnl.Controls.Add(txtbox); Button btn; btn = new Button(); btn.Text = "Submit control 1"; btn.Click += new EventHandler(btn_Click); btn.ID = UniqueID +1; pnl.Controls.Add(btn); btn = new Button(); btn.Text = "Submit control 2"; btn.Click += new EventHandler(btn_Click); btn.ID = UniqueID +2; pnl.Controls.Add(btn); base.CreateChildControls(); } private void btn_Click(object sender, EventArgs e) { System.Web.HttpContext.Current.Response.Write("******GOT THE Button Click from btn_Click !!!!*****"); System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click from btn_Click !!!!*****"); } /// <summary> /// Render this control to the output parameter specified. /// </summary> /// <param name="output"> The HTML writer to write out to </param> protected override void Render(HtmlTextWriter output) { System.Web.HttpContext.Current.Trace.Write("Render()"); pnl.RenderControl(output); } public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { foreach (string s in postCollection) { System.Web.HttpContext.Current.Response.Write(s + " : value " + postCollection.Get(s) + "<br>"); } String presentValue = txtFirstName; String postedValue = postCollection[postDataKey]; System.Web.HttpContext.Current.Response.Write(postDataKey.ToString()); if (presentValue == null || !presentValue.Equals(postedValue)) { txtFirstName = postedValue; return true; } return false; } public virtual void RaisePostDataChangedEvent() { System.Web.HttpContext.Current.Response.Write("******GOT THE Event from RaisePostDataChangedEvent!!!!*****<br>"); System.Web.HttpContext.Current.Response.Write("Need to deciede which control event raised the postback<br>"); Control control = null; string ctrlname = Page.Request.Params.Get("__EVENTTARGET"); if (ctrlname != null && ctrlname != string.Empty) { control = Page.FindControl(ctrlname); } else { foreach (string ctl in Page.Request.Form) { Control c = Page.FindControl(ctl); if (c is System.Web.UI.WebControls.Button) { control = c; break; } } } //System.Web.HttpContext.Current.Response.Write(ctrlname.ToString()); //OnValueChanged(EventArgs.Empty); } } } Hi,
because the control contains postbacking child controls, it would need to implement INamingContainer interface. -- Show quoteHide quoteTeemu Keiski ASP.NET MVP, AspInsider Finland, EU "Martin" <martin_no_spam@martinz.co.nz> wrote in message news:OPDr3UEWFHA.228@TK2MSFTNGP12.phx.gbl... > Hi, > > I have created a composite control that has a number of standard asp.net > controls on it that can themselves cause postbacks. > What i need to do in my composite control is to determine which consituent > control caused a postback. > > for example a have a consituent controls with two buttons on it "button1" > and "button2" > I have registered my control for postbacks using > "Page.RegisterRequiresPostBack(this);" > > I have include a dynamic handler for my buttons and registered this > however > when I click the button the code in my dynamic handler does not get > raised. > > however, because I have implemented "IPostBackDataHandler " the event > "RaisePostDataChangedEvent()" does get raised however this seems to have > no > information in it about which button was clicked all it tells me is that a > postback occured. > > what ideally i need to do is get the dynamic button handlesr to work, can > any body give me any advice on this or point me to a good article that > shows > how to run button handlers from within compsoite controls. > i have included my code below for anybody's reference. -- this is my > entire > test user control. > > many thanks in advance. > > cheers > > martin. > > using System; > > using System.Web.UI; > > using System.Web.UI.WebControls; > > using System.ComponentModel; > > using System.Collections.Specialized; > > namespace CompositeControls > > { > > /// <summary> > > /// Summary description for Compositecontrol. > > /// </summary> > > [DefaultProperty("Text"), > > ToolboxData("<{0}:Compositecontrol runat=server></{0}:Compositecontrol>")] > > public class Compositecontrol : System.Web.UI.WebControls.WebControl, > IPostBackDataHandler > > { > > private Panel pnl; > > private string text; > > > [Bindable(true), > > Category("Appearance"), > > DefaultValue("")] > > public string txtFirstName > > { > > get > > { > > return text; > > } > > set > > { > > text = value; > > } > > } > > protected override void OnInit(EventArgs e) > > { > > base.OnInit(e); > > if (Page != null) > > Page.RegisterRequiresPostBack(this); > > } > > > > protected override void CreateChildControls() > > { > > System.Web.HttpContext.Current.Trace.Write("CreateChildControls()"); > > pnl = new Panel(); > > TextBox txtbox = new TextBox(); > > txtbox.ID = UniqueID; > > txtbox.Text = txtFirstName; > > pnl.Controls.Add(txtbox); > > Button btn; > > btn = new Button(); > > btn.Text = "Submit control 1"; > > btn.Click += new EventHandler(btn_Click); > > btn.ID = UniqueID +1; > > pnl.Controls.Add(btn); > > btn = new Button(); > > btn.Text = "Submit control 2"; > > btn.Click += new EventHandler(btn_Click); > > btn.ID = UniqueID +2; > > pnl.Controls.Add(btn); > > base.CreateChildControls(); > > } > > private void btn_Click(object sender, EventArgs e) > > { > > System.Web.HttpContext.Current.Response.Write("******GOT THE Button Click > from btn_Click !!!!*****"); > > System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click > from > btn_Click !!!!*****"); > > } > > /// <summary> > > /// Render this control to the output parameter specified. > > /// </summary> > > /// <param name="output"> The HTML writer to write out to </param> > > protected override void Render(HtmlTextWriter output) > > { > > System.Web.HttpContext.Current.Trace.Write("Render()"); > > pnl.RenderControl(output); > > } > > public virtual bool LoadPostData(string postDataKey, > > NameValueCollection postCollection) > > { > > foreach (string s in postCollection) > > { > > System.Web.HttpContext.Current.Response.Write(s + " : value " + > postCollection.Get(s) + "<br>"); > > } > > String presentValue = txtFirstName; > > String postedValue = postCollection[postDataKey]; > > System.Web.HttpContext.Current.Response.Write(postDataKey.ToString()); > > if (presentValue == null || !presentValue.Equals(postedValue)) > > { > > > txtFirstName = postedValue; > > return true; > > } > > return false; > > } > > > > public virtual void RaisePostDataChangedEvent() > > { > > System.Web.HttpContext.Current.Response.Write("******GOT THE Event from > RaisePostDataChangedEvent!!!!*****<br>"); > > System.Web.HttpContext.Current.Response.Write("Need to deciede which > control > event raised the postback<br>"); > > Control control = null; > > string ctrlname = Page.Request.Params.Get("__EVENTTARGET"); > > if (ctrlname != null && ctrlname != string.Empty) > > { > > control = Page.FindControl(ctrlname); > > } > > else > > { > > foreach (string ctl in Page.Request.Form) > > { > > Control c = Page.FindControl(ctl); > > if (c is System.Web.UI.WebControls.Button) > > { > > control = c; > > break; > > } > > } > > } > > //System.Web.HttpContext.Current.Response.Write(ctrlname.ToString()); > > //OnValueChanged(EventArgs.Empty); > > } > > } > > } > > > > > Hi Teemu,
thanks for the reply, I just done a quick search on INamingContainer and the document i found said "This is a marker interface only." I am not quite sure what this means. can you offer any further advice or pointers in the right direction. cheers martin. Show quoteHide quote "Teemu Keiski" <jot***@aspalliance.com> wrote in message runat=server></{0}:Compositecontrol>")]news:e%238bR2EWFHA.3216@TK2MSFTNGP10.phx.gbl... > Hi, > > because the control contains postbacking child controls, it would need to > implement INamingContainer interface. > > -- > Teemu Keiski > ASP.NET MVP, AspInsider > Finland, EU > > > "Martin" <martin_no_spam@martinz.co.nz> wrote in message > news:OPDr3UEWFHA.228@TK2MSFTNGP12.phx.gbl... > > Hi, > > > > I have created a composite control that has a number of standard asp.net > > controls on it that can themselves cause postbacks. > > What i need to do in my composite control is to determine which consituent > > control caused a postback. > > > > for example a have a consituent controls with two buttons on it "button1" > > and "button2" > > I have registered my control for postbacks using > > "Page.RegisterRequiresPostBack(this);" > > > > I have include a dynamic handler for my buttons and registered this > > however > > when I click the button the code in my dynamic handler does not get > > raised. > > > > however, because I have implemented "IPostBackDataHandler " the event > > "RaisePostDataChangedEvent()" does get raised however this seems to have > > no > > information in it about which button was clicked all it tells me is that a > > postback occured. > > > > what ideally i need to do is get the dynamic button handlesr to work, can > > any body give me any advice on this or point me to a good article that > > shows > > how to run button handlers from within compsoite controls. > > i have included my code below for anybody's reference. -- this is my > > entire > > test user control. > > > > many thanks in advance. > > > > cheers > > > > martin. > > > > using System; > > > > using System.Web.UI; > > > > using System.Web.UI.WebControls; > > > > using System.ComponentModel; > > > > using System.Collections.Specialized; > > > > namespace CompositeControls > > > > { > > > > /// <summary> > > > > /// Summary description for Compositecontrol. > > > > /// </summary> > > > > [DefaultProperty("Text"), > > > > ToolboxData("<{0}:Compositecontrol Show quoteHide quote > > > > public class Compositecontrol : System.Web.UI.WebControls.WebControl, > > IPostBackDataHandler > > > > { > > > > private Panel pnl; > > > > private string text; > > > > > > [Bindable(true), > > > > Category("Appearance"), > > > > DefaultValue("")] > > > > public string txtFirstName > > > > { > > > > get > > > > { > > > > return text; > > > > } > > > > set > > > > { > > > > text = value; > > > > } > > > > } > > > > protected override void OnInit(EventArgs e) > > > > { > > > > base.OnInit(e); > > > > if (Page != null) > > > > Page.RegisterRequiresPostBack(this); > > > > } > > > > > > > > protected override void CreateChildControls() > > > > { > > > > System.Web.HttpContext.Current.Trace.Write("CreateChildControls()"); > > > > pnl = new Panel(); > > > > TextBox txtbox = new TextBox(); > > > > txtbox.ID = UniqueID; > > > > txtbox.Text = txtFirstName; > > > > pnl.Controls.Add(txtbox); > > > > Button btn; > > > > btn = new Button(); > > > > btn.Text = "Submit control 1"; > > > > btn.Click += new EventHandler(btn_Click); > > > > btn.ID = UniqueID +1; > > > > pnl.Controls.Add(btn); > > > > btn = new Button(); > > > > btn.Text = "Submit control 2"; > > > > btn.Click += new EventHandler(btn_Click); > > > > btn.ID = UniqueID +2; > > > > pnl.Controls.Add(btn); > > > > base.CreateChildControls(); > > > > } > > > > private void btn_Click(object sender, EventArgs e) > > > > { > > > > System.Web.HttpContext.Current.Response.Write("******GOT THE Button Click > > from btn_Click !!!!*****"); > > > > System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click > > from > > btn_Click !!!!*****"); > > > > } > > > > /// <summary> > > > > /// Render this control to the output parameter specified. > > > > /// </summary> > > > > /// <param name="output"> The HTML writer to write out to </param> > > > > protected override void Render(HtmlTextWriter output) > > > > { > > > > System.Web.HttpContext.Current.Trace.Write("Render()"); > > > > pnl.RenderControl(output); > > > > } > > > > public virtual bool LoadPostData(string postDataKey, > > > > NameValueCollection postCollection) > > > > { > > > > foreach (string s in postCollection) > > > > { > > > > System.Web.HttpContext.Current.Response.Write(s + " : value " + > > postCollection.Get(s) + "<br>"); > > > > } > > > > String presentValue = txtFirstName; > > > > String postedValue = postCollection[postDataKey]; > > > > System.Web.HttpContext.Current.Response.Write(postDataKey.ToString()); > > > > if (presentValue == null || !presentValue.Equals(postedValue)) > > > > { > > > > > > txtFirstName = postedValue; > > > > return true; > > > > } > > > > return false; > > > > } > > > > > > > > public virtual void RaisePostDataChangedEvent() > > > > { > > > > System.Web.HttpContext.Current.Response.Write("******GOT THE Event from > > RaisePostDataChangedEvent!!!!*****<br>"); > > > > System.Web.HttpContext.Current.Response.Write("Need to deciede which > > control > > event raised the postback<br>"); > > > > Control control = null; > > > > string ctrlname = Page.Request.Params.Get("__EVENTTARGET"); > > > > if (ctrlname != null && ctrlname != string.Empty) > > > > { > > > > control = Page.FindControl(ctrlname); > > > > } > > > > else > > > > { > > > > foreach (string ctl in Page.Request.Form) > > > > { > > > > Control c = Page.FindControl(ctl); > > > > if (c is System.Web.UI.WebControls.Button) > > > > { > > > > control = c; > > > > break; > > > > } > > > > } > > > > } > > > > //System.Web.HttpContext.Current.Response.Write(ctrlname.ToString()); > > > > //OnValueChanged(EventArgs.Empty); > > > > } > > > > } > > > > } > > > > > > > > > > > > It means that you don't implement any methods when implementing it
(interface doesn't include any methods, it just "marks" the type). Just add the declaration. public class Compositecontrol : System.Web.UI.WebControls.WebControl, IPostBackDataHandler, INamingContainer -- Show quoteHide quoteTeemu Keiski ASP.NET MVP, AspInsider Finland, EU "Martin" <martin_no_spam@martinz.co.nz> wrote in message news:O$f0pNFWFHA.1508@tk2msftngp13.phx.gbl... > Hi Teemu, > > thanks for the reply, > I just done a quick search on INamingContainer and the document i found > said "This is a marker interface only." > I am not quite sure what this means. > > can you offer any further advice or pointers in the right direction. > > cheers > > martin. > > "Teemu Keiski" <jot***@aspalliance.com> wrote in message > news:e%238bR2EWFHA.3216@TK2MSFTNGP10.phx.gbl... >> Hi, >> >> because the control contains postbacking child controls, it would need to >> implement INamingContainer interface. >> >> -- >> Teemu Keiski >> ASP.NET MVP, AspInsider >> Finland, EU >> >> >> "Martin" <martin_no_spam@martinz.co.nz> wrote in message >> news:OPDr3UEWFHA.228@TK2MSFTNGP12.phx.gbl... >> > Hi, >> > >> > I have created a composite control that has a number of standard >> > asp.net >> > controls on it that can themselves cause postbacks. >> > What i need to do in my composite control is to determine which > consituent >> > control caused a postback. >> > >> > for example a have a consituent controls with two buttons on it > "button1" >> > and "button2" >> > I have registered my control for postbacks using >> > "Page.RegisterRequiresPostBack(this);" >> > >> > I have include a dynamic handler for my buttons and registered this >> > however >> > when I click the button the code in my dynamic handler does not get >> > raised. >> > >> > however, because I have implemented "IPostBackDataHandler " the event >> > "RaisePostDataChangedEvent()" does get raised however this seems to >> > have >> > no >> > information in it about which button was clicked all it tells me is >> > that > a >> > postback occured. >> > >> > what ideally i need to do is get the dynamic button handlesr to work, > can >> > any body give me any advice on this or point me to a good article that >> > shows >> > how to run button handlers from within compsoite controls. >> > i have included my code below for anybody's reference. -- this is my >> > entire >> > test user control. >> > >> > many thanks in advance. >> > >> > cheers >> > >> > martin. >> > >> > using System; >> > >> > using System.Web.UI; >> > >> > using System.Web.UI.WebControls; >> > >> > using System.ComponentModel; >> > >> > using System.Collections.Specialized; >> > >> > namespace CompositeControls >> > >> > { >> > >> > /// <summary> >> > >> > /// Summary description for Compositecontrol. >> > >> > /// </summary> >> > >> > [DefaultProperty("Text"), >> > >> > ToolboxData("<{0}:Compositecontrol > runat=server></{0}:Compositecontrol>")] >> > >> > public class Compositecontrol : System.Web.UI.WebControls.WebControl, >> > IPostBackDataHandler >> > >> > { >> > >> > private Panel pnl; >> > >> > private string text; >> > >> > >> > [Bindable(true), >> > >> > Category("Appearance"), >> > >> > DefaultValue("")] >> > >> > public string txtFirstName >> > >> > { >> > >> > get >> > >> > { >> > >> > return text; >> > >> > } >> > >> > set >> > >> > { >> > >> > text = value; >> > >> > } >> > >> > } >> > >> > protected override void OnInit(EventArgs e) >> > >> > { >> > >> > base.OnInit(e); >> > >> > if (Page != null) >> > >> > Page.RegisterRequiresPostBack(this); >> > >> > } >> > >> > >> > >> > protected override void CreateChildControls() >> > >> > { >> > >> > System.Web.HttpContext.Current.Trace.Write("CreateChildControls()"); >> > >> > pnl = new Panel(); >> > >> > TextBox txtbox = new TextBox(); >> > >> > txtbox.ID = UniqueID; >> > >> > txtbox.Text = txtFirstName; >> > >> > pnl.Controls.Add(txtbox); >> > >> > Button btn; >> > >> > btn = new Button(); >> > >> > btn.Text = "Submit control 1"; >> > >> > btn.Click += new EventHandler(btn_Click); >> > >> > btn.ID = UniqueID +1; >> > >> > pnl.Controls.Add(btn); >> > >> > btn = new Button(); >> > >> > btn.Text = "Submit control 2"; >> > >> > btn.Click += new EventHandler(btn_Click); >> > >> > btn.ID = UniqueID +2; >> > >> > pnl.Controls.Add(btn); >> > >> > base.CreateChildControls(); >> > >> > } >> > >> > private void btn_Click(object sender, EventArgs e) >> > >> > { >> > >> > System.Web.HttpContext.Current.Response.Write("******GOT THE Button > Click >> > from btn_Click !!!!*****"); >> > >> > System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click >> > from >> > btn_Click !!!!*****"); >> > >> > } >> > >> > /// <summary> >> > >> > /// Render this control to the output parameter specified. >> > >> > /// </summary> >> > >> > /// <param name="output"> The HTML writer to write out to </param> >> > >> > protected override void Render(HtmlTextWriter output) >> > >> > { >> > >> > System.Web.HttpContext.Current.Trace.Write("Render()"); >> > >> > pnl.RenderControl(output); >> > >> > } >> > >> > public virtual bool LoadPostData(string postDataKey, >> > >> > NameValueCollection postCollection) >> > >> > { >> > >> > foreach (string s in postCollection) >> > >> > { >> > >> > System.Web.HttpContext.Current.Response.Write(s + " : value " + >> > postCollection.Get(s) + "<br>"); >> > >> > } >> > >> > String presentValue = txtFirstName; >> > >> > String postedValue = postCollection[postDataKey]; >> > >> > System.Web.HttpContext.Current.Response.Write(postDataKey.ToString()); >> > >> > if (presentValue == null || !presentValue.Equals(postedValue)) >> > >> > { >> > >> > >> > txtFirstName = postedValue; >> > >> > return true; >> > >> > } >> > >> > return false; >> > >> > } >> > >> > >> > >> > public virtual void RaisePostDataChangedEvent() >> > >> > { >> > >> > System.Web.HttpContext.Current.Response.Write("******GOT THE Event from >> > RaisePostDataChangedEvent!!!!*****<br>"); >> > >> > System.Web.HttpContext.Current.Response.Write("Need to deciede which >> > control >> > event raised the postback<br>"); >> > >> > Control control = null; >> > >> > string ctrlname = Page.Request.Params.Get("__EVENTTARGET"); >> > >> > if (ctrlname != null && ctrlname != string.Empty) >> > >> > { >> > >> > control = Page.FindControl(ctrlname); >> > >> > } >> > >> > else >> > >> > { >> > >> > foreach (string ctl in Page.Request.Form) >> > >> > { >> > >> > Control c = Page.FindControl(ctl); >> > >> > if (c is System.Web.UI.WebControls.Button) >> > >> > { >> > >> > control = c; >> > >> > break; >> > >> > } >> > >> > } >> > >> > } >> > >> > //System.Web.HttpContext.Current.Response.Write(ctrlname.ToString()); >> > >> > //OnValueChanged(EventArgs.Empty); >> > >> > } >> > >> > } >> > >> > } >> > >> > >> > >> > >> > >> >> > >
Other interesting topics
Dynamically Adding Controls
Updating a DB via SQL is giving me a headache Reading IP.... thread.start() does not actually execute the assigned method Redirecting to a page with a querystring and an anchor reference Uploading graphics. DropDownList Control Problem with UrlMappings HttpException (0x80070005): Access denied to 'c:\inetpub' link text box to datasource |
|||||||||||||||||||||||