manually call a methodT

K

Karl Hungus

Im using an xsl transformation so its not practical to use controls in the
usual way, but I would still like to call methods in my codebehind class.

usually Id just use onclick="<eventHandler>" say for something like a button
control

is there a way to manually call a method defined in a codebehind class, but
is not called by a standard control? I looked at the __doPostBack() function
that .net generates but it seemed impenetrable.

TIA
karl
 
K

Kevin Spencer

The "CodeBehind Class" is your Page class (the .aspx Page inherits it). Just
refer to it from any Control as "Page" - which is a member of all
System.Web.UI.Controls. Example:

Page.DoSomething(someParameter);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
K

Karl Hungus

thats the problem. I cant "just refer to it from any control" Im not able to
use controls because of an XSL transformation. I need to manually recreate
the __doPostBack js function. Im just not sure if its possible to get to the
method that way.
 
S

Scott

I use a XSLT to transform some XML to HTML; when I want to do a postback inside the HTML output I
include a parameter to the transform --

TransformArgumentList.AddParam("postback", "", this.Page.GetPostBackClientHyperlink(this,
"ARG"));
-- do the transform....

Then in the XSLT I construct an anchor; off the top of my head mine looks something like

<xslt:template match="whatever">
<a>
<xslt:attribute name="href">
construct-the-href-using the param "postback"
</xslt:attribute>
</a>
</xslt:template>

Now for this to work you have to have an enclosing control that implements IPostBackEventHandler
(that's the first argument to the GetPostBackClientHyperLink; where the interesting method is
RaisePostBackEvent(string eventArgument). The eventArgument is the selector that you can use in your
generated HTML to differentiate what's what (in this case it's ARG, however in your XSLT rule you
can change it to whatever you want).

I guess if you were really ambitious you could also write a page like parser (similar to the
ASCX/ASPX) that would allow you to associate a method at runtime... a lot of work.

Scott
 
K

Karl Hungus

OK, this is good.

Im following you except for the part about the enclosing control. Im not
clear on what it's enclosing. Is it the entire transformation, or the page
or...?

Thanks,
Karl


Scott said:
I use a XSLT to transform some XML to HTML; when I want to do a postback inside the HTML output I
include a parameter to the transform --

TransformArgumentList.AddParam("postback", "", this.Page.GetPostBackClientHyperlink(this,
"ARG"));
-- do the transform....

Then in the XSLT I construct an anchor; off the top of my head mine looks something like

<xslt:template match="whatever">
<a>
<xslt:attribute name="href">
construct-the-href-using the param "postback"
</xslt:attribute>
</a>
</xslt:template>

Now for this to work you have to have an enclosing control that
implements IPostBackEventHandler
(that's the first argument to the GetPostBackClientHyperLink; where the interesting method is
RaisePostBackEvent(string eventArgument). The eventArgument is the
selector that you can use in your
 
S

Scott

You need a control for the target of the postback; here's a skeleton of what I have; in the code
below, the XmlHtmlControl is my own version of the builtin Xml class (it does basically the same
thing, but it I didn't like the built in version). You should play around and see what works best
for you -- if you look at the generated source, you ought to be able to visually check the
__doPostBack calls to see if you are doing the right thing. Good luck. (I'm typing this in OE....
it's just an example).


public MyControl : Control, IPostBackEventHandler {
XmlHtmlControl XmlCtrl;
public override ControlCollection Controls {
get {
this.EnsureChildControls();
return base.Controls();
}
}
protected override void CreateChildControls() {
this.Controls.Clear();
XmlCtrl = new XmlHtmlControl();
this.Controls.Add(XmlCtrl);
this.ChildControlsCreated = true;
}

// in my class I'm using DataBind(); but you can do this wherever you want
public override void DataBind() {
XslTransform xslt = new XsltTransform();
xslt.Load(Server.MapPath("~/foobar.xslt"));
XmlCtrl.Transform = xslt;
XmlCtrl.DocumentSource = Server.MapPath("~/doc.xml");
string postback = this.Page.GetPostBackClientHyperLink(this, "ARG");
XmlCtrl.TransformArgumentList.Clear();
XmlCtrl.TransformArgumentList.AddParam("postback", "", postback);
base.DataBind();
}

// declare some kind of event if you want "Click"?

public void RaisePostBackEvent(string eventArgument) {
// do something with eventArgument.... perhaps controls some kind of EventArgs subclass
// then call some kind of event handler or what ever...
// perhaps call a "Click" event.
}
}
 
K

Karl Hungus

Well, that was extremely helpful. And I got to write my first custom
control.

thanks!
Karl
 

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

Top