Custom onclick event for composite control

A

AndrewF

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
 
T

Teemu Keiski

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top