Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Custom onclick event for composite control

Reply
Thread Tools

Custom onclick event for composite control

 
 
AndrewF
Guest
Posts: n/a
 
      08-06-2005
Hi there.

This has been posted to a couple of other groups where it is relevant
as well as I am royally stuck with this so any help would be greatly
appreciated.


Basically I have a set of classes that create composite controls for
the user. One of these is an upload object so users can upload files to

the server as part of the CMS.


The composite control contains:


HtmlInputFile object to get the path name,
Somer literal controls holding some bumf HTML to tell the user what
they have uploaded etc.
a Button control called funnily enough "Upload".


Now if these items were just dumped onto a page at design time I
wouldn't have a problem as I could wire up the server onclick event
without any drama at all as I could whack it in the tag - this is all
built at runtime however and it doesn't appear to be that simple.


I have got a class thus:


// infControls.File.cs
//


namespace InfControls {


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Specialized*;


/// <summary>
/// infFile is the basic file upload option. It is a composite
class
/// consiting of the file upload object itself as well as some
/// basic information underneath it as well about what has been

uploaded
/// or not as the case may be.
/// </summary>
[ValidationPropertyAttribute("*Text")]
public class infFile : Control, INamingContainer,
IPostBackDataHandler {


// make _name accessible at any point.
string _name;
string _filepath;


public infFile(string name) {
_name = name;
}


/// <summary> create an instance and set the file path
/// as well </summary>
public infFile(string name, string filepath) {
_name = name;
_filepath = filepath;
}


private void upload_click(Object sender, EventArgs e) {


//Button clicked = (Button)sender;


//HtmlInputFile thefile =
(HtmlInputFile)FindControl(_na*me + "_up");


this.Text += "this is a test";
}


protected override void CreateChildControls() {


// set up the controls we want.
LiteralControl text;
LiteralControl disptext;


// used for the actual file to be uplaoded on
HtmlInputFile file = new HtmlInputFile();
file.ID = _name + "_up";
Controls.Add(file);


// add the button that does the upload
Button uploadbtn = new Button();
uploadbtn.ID = _name + "_btn";
uploadbtn.Text = "Upload";
uploadbtn.Click += new EventHandler(upload_click);
Controls.Add(uploadbtn);


// add some text surrounding it all.
text = new LiteralControl("<br/>");
Controls.Add(text);


// so now depending on whether we
if (_filepath== "") {
disptext = new LiteralControl("No file has been
uploaded");
Controls.Add(disptext);
} else {
disptext = new LiteralControl("Currently uploaded:
");
Controls.Add(disptext);


HyperLink hl = new HyperLink();
hl.Text = _filepath;
hl.NavigateUrl = _filepath;
hl.Target = "newwin";
Controls.Add(hl);
}
}


// define some public methods etc to maintain state.
public string Text {
get {
if (ViewState["value"] == null)
return String.Empty;
return (string) ViewState["value"];
}
set {
ViewState["value"] = value;
_filepath = value;
}
}


// this is where we implement the iPostBackDataHandler
Interface
bool IPostBackDataHandler.LoadPostD*ata( string
postDataKey,
NameValueCollection

postCollection) {


Text = postCollection[postDataKey];
return false;
}


void IPostBackDataHandler.RaisePost*DataChangedEvent(){ }
}
}


As can be seen in the constructor, I am adding the event without any
drama and there doesn't seem to be a problem with it in the compiler.
It just doesn't seem to be firing in the page when I click on the
button.


In the form when it displays, it shows up in the right spot, the form
is submitted and a post back is done and no text appears in the label I

am using to manage the text with.


I know I am doing something wrong because the test case using design
controls works great - but where I am going wrong, I have no idea so
any help would be greatly appreciated.


Cheers
AndrewF

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      08-07-2005
Hi,

because you implement IPostBackDataHandler it goes "over" the postbacking
control's (Button) postback events which won't be raised by default. This is
typical case when control deals with postback data and postback events, and
it needs a bit more work from control developer's part.

You need to in LoadPostData check from the postCollection if your Button
control's UniqueID exists in the collection and if it does, call

Page.RegisterRequiresRaiseEvent(btnInstance);

which registers the Button to raise the postback event

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"AndrewF" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
Hi there.

This has been posted to a couple of other groups where it is relevant
as well as I am royally stuck with this so any help would be greatly
appreciated.


Basically I have a set of classes that create composite controls for
the user. One of these is an upload object so users can upload files to

the server as part of the CMS.


The composite control contains:


HtmlInputFile object to get the path name,
Somer literal controls holding some bumf HTML to tell the user what
they have uploaded etc.
a Button control called funnily enough "Upload".


Now if these items were just dumped onto a page at design time I
wouldn't have a problem as I could wire up the server onclick event
without any drama at all as I could whack it in the tag - this is all
built at runtime however and it doesn't appear to be that simple.


I have got a class thus:


// infControls.File.cs
//


namespace InfControls {


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Specialized*;


/// <summary>
/// infFile is the basic file upload option. It is a composite
class
/// consiting of the file upload object itself as well as some
/// basic information underneath it as well about what has been

uploaded
/// or not as the case may be.
/// </summary>
[ValidationPropertyAttribute("*Text")]
public class infFile : Control, INamingContainer,
IPostBackDataHandler {


// make _name accessible at any point.
string _name;
string _filepath;


public infFile(string name) {
_name = name;
}


/// <summary> create an instance and set the file path
/// as well </summary>
public infFile(string name, string filepath) {
_name = name;
_filepath = filepath;
}


private void upload_click(Object sender, EventArgs e) {


//Button clicked = (Button)sender;


//HtmlInputFile thefile =
(HtmlInputFile)FindControl(_na*me + "_up");


this.Text += "this is a test";
}


protected override void CreateChildControls() {


// set up the controls we want.
LiteralControl text;
LiteralControl disptext;


// used for the actual file to be uplaoded on
HtmlInputFile file = new HtmlInputFile();
file.ID = _name + "_up";
Controls.Add(file);


// add the button that does the upload
Button uploadbtn = new Button();
uploadbtn.ID = _name + "_btn";
uploadbtn.Text = "Upload";
uploadbtn.Click += new EventHandler(upload_click);
Controls.Add(uploadbtn);


// add some text surrounding it all.
text = new LiteralControl("<br/>");
Controls.Add(text);


// so now depending on whether we
if (_filepath== "") {
disptext = new LiteralControl("No file has been
uploaded");
Controls.Add(disptext);
} else {
disptext = new LiteralControl("Currently uploaded:
");
Controls.Add(disptext);


HyperLink hl = new HyperLink();
hl.Text = _filepath;
hl.NavigateUrl = _filepath;
hl.Target = "newwin";
Controls.Add(hl);
}
}


// define some public methods etc to maintain state.
public string Text {
get {
if (ViewState["value"] == null)
return String.Empty;
return (string) ViewState["value"];
}
set {
ViewState["value"] = value;
_filepath = value;
}
}


// this is where we implement the iPostBackDataHandler
Interface
bool IPostBackDataHandler.LoadPostD*ata( string
postDataKey,
NameValueCollection

postCollection) {


Text = postCollection[postDataKey];
return false;
}


void IPostBackDataHandler.RaisePost*DataChangedEvent(){ }
}
}


As can be seen in the constructor, I am adding the event without any
drama and there doesn't seem to be a problem with it in the compiler.
It just doesn't seem to be firing in the page when I click on the
button.


In the form when it displays, it shows up in the right spot, the form
is submitted and a post back is done and no text appears in the label I

am using to manage the text with.


I know I am doing something wrong because the test case using design
controls works great - but where I am going wrong, I have no idea so
any help would be greatly appreciated.


Cheers
AndrewF


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom onclick event for composite control AndrewF ASP .Net Web Controls 1 08-07-2005 02:30 PM
Custom onclick event for composite control AndrewF ASP .Net 0 08-05-2005 03:05 PM
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Using Table control in a custom composite control. Control does not render properly in design time. jb_in_marietta@yahoo.com ASP .Net 0 07-01-2003 09:26 PM



Advertisments