M
Martin
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);
}
}
}
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);
}
}
}