473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Composite Controls, eventhandlers and knowing which control caused postback

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("Create ChildControls()");

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(post DataKey.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("Nee d 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(ctrl name.ToString());

//OnValueChanged(EventArgs.Empty);

}

}

}

Nov 19 '05 #1
3 2541
Hi,

because the control contains postbacking child controls, it would need to
implement INamingContainer interface.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Martin" <ma************@martinz.co.nz> wrote in message
news:OP*************@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("Create ChildControls()");

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(post DataKey.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("Nee d 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(ctrl name.ToString());

//OnValueChanged(EventArgs.Empty);

}

}

}


Nov 19 '05 #2
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" <jo****@aspalliance.com> wrote in message
news:e%****************@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" <ma************@martinz.co.nz> wrote in message
news:OP*************@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("Create ChildControls()");

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(post DataKey.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("Nee d 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(ctrl name.ToString());

//OnValueChanged(EventArgs.Empty);

}

}

}



Nov 19 '05 #3
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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU

"Martin" <ma************@martinz.co.nz> wrote in message
news:O$**************@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" <jo****@aspalliance.com> wrote in message
news:e%****************@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" <ma************@martinz.co.nz> wrote in message
news:OP*************@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("Create ChildControls()");
>
> 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(post DataKey.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("Nee d 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(ctrl name.ToString());
>
> //OnValueChanged(EventArgs.Empty);
>
> }
>
> }
>
> }
>
>
>
>
>



Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1642
by: Jeff Voigt | last post by:
Is there any way to dynamically get the name of the control that caused the postback? Since SmartNav is not working for me I'm trying to implement a way to scroll to the control that caused the...
10
2295
by: dx | last post by:
I have the Microsoft Press: Developing Microsoft ASP.NET Server Controls and Components book. It's starting to shine some light on control development but there is something about composite...
4
1530
by: PokerMan | last post by:
Hi I have a few controls on apage that cause a postback. But want to handle a postback differently depending on which one of these controls fired the postback. How do we do this? c#. Thanks
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.