How can I use working Linkbuttons within a Web Custom Control?

J

Jan Limpens

hi!

I've got a Web Custom Control within which I'd like to use LinkButtons.
This looks more ore less like this...

protected override void Render(HtmlTextWriter output)
{
output.WriteLine();
output.WriteFullBeginTag("P");
LinkButton b = new LinkButton();
b.Text = "Bla";
b.RenderControl(output);
output.WriteEndTag("P");
}

As a result I get a HTML Link which is way to plain...

<P><a>Bla</a></P>

There is no functionality behind this, no javascript to handle the
postback, no id, no nothing....
Even setting Command Names, Arguments and Click Events does not help...

Any help? How can I use working Linkbuttons within a Web Custom Control?

Jan
 
S

Steve Drake

try

NOTE the lines with a *, these is the bits your were missing to make it
work.


LinkButton yourLinkButton ;

public myClass : WebControl , INamingContainer // * Must Implement
INamingContainer,

// its just a marker interface, very COM like,

//why didn't they use an attribute here ?

// I presume its faster, eg just have code like if (object is
INamingContainer)

protected override void CreateChildControls() // * I believe the property
way is not to create the controls in the render
{
yourLinkButton = new LinkButton();
yourLinkButton.Text = "Press me";

// * if you don't hook into the event why would it generate the
postback script?
// try typing this line, .NET 2003 will write the code for you if you
press tab just after you type +=, hit tab again and it writes the code at
the bottom.
yourLinkButton.Command+=new
System.Web.UI.WebControls.CommandEventHandler(yourLinkButton_Command);

controls.Add(yourLinkButton); // * Add it to the controls collection.
}

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls(); // This will run the CreateChildControls() if its
needed.
yourLinkButton .RenderControl(writer);
}

private void yourLinkButton_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
// HANDLE YOUR EVENT
}
 
J

Jan Limpens

hi steve,

it works :)!
and the gizmo with tabbing the event code, whow, i´ll print that on a
tshirt for more people to know ;). that´s a big timesaver! you don´t
happen to know anything similar for the creation of standard get/set
properties, do you?

thanks a lot
jan
 
S

Steve Drake

no for get sets, but you can write a macro.

the tab thing also works for interfaces, type the interface name, hit tab
and it put the code in for you.

Also.... when you need to override from a base class, just type override,
you will see a list of things you can override, hit tab and it will put the
code in for you.

You will need a big t-shirt add all those tips :)

Steve
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top