Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: RenderEndTag, LinkButton and custom CommandEventHandler


Message #1 by Feduke Cntr Charles R <FedukeCR@m...> on Thu, 25 Apr 2002 10:46:07 -0400
	I'm having a problem with getting a LinkButton to actually create an
href.  I'm writing a class that inherits from WebControls.DropDownList, and
essentially tacking some <td>s around a caption and the actual drop down
list.  This class also has an "AllowAddNew" bool property, that when it is
true, should create a link that can be clicked on to raise the OnAddCommand
event of my class.  I'm trying to do all this magic in the RenderEndTag
event:

public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			writer.WriteLine("</select>");
			if (_allowAddNew)
			{
				LinkButton lb = new LinkButton();
				lb.Command += AddCommand;
				lb.Text = _addNewCaption;
				lb.RenderControl(writer);
				if (AddCommand != null)
					writer.Write(" Add Command is not
null.");
			}
			writer.WriteLine("</td>");
		}

	What I am getting is a select box followed by a <a>New...</a>, but
no href, and the " Add Command is not null." statement.  So I know the
AddCommand command event handler has a value and _allowAddNew is true, but
neither .Command nor .Click seem to work for LinkButton or Button controls
in the RenderEndTag event.

- Chuck
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Thu, 25 Apr 2002 15:44:41 -0400
	An answer to my own question.  Since I know someone will run across
this problem in the future, here's some information on it.

	From a posting and e-mail conversation I had off list with Geoff
Taylor, I learned a bit more about the order of events in an ASPX page and
why I can't arbitrarily add a control that requires an event during the
RenderXXX events - this is because the control's event won't have an entry
in the event table so I'd be calling a null event.

	Essentially my control outputs a DropDownList that populates itself
from a SQL Select statement.  Optionally the developer/consumer of the
control could allow the user to add a new item to the control, so I wanted
to generate a clickable LinkButton with a hookable OnAddCommand event (and
once this OnAddCommand event fires, the developer could display additional
controls for adding a new item to the list).

	Code snippets appear below:

private LinkButton add;

protected override void OnInit(System.EventArgs e)
		{
			// create our link button for add new command
			add = new LinkButton();
			add.Text = _addNewCaption;
			add.Command += AddCommand;
			// these next two lines are magic
			Page.RegisterRequiresRaiseEvent(add);
			this.AddedControl(add, 0); // help says don't use
this
		}

public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			/* note: base.RenderEndTag is not called here
				so we manually write out the '</select>'
			*/
			writer.WriteLine("</select>");
			if (_allowAddNew)
				add.RenderControl(writer);
			// my RenderStartTag starts a '<td>'
			writer.WriteLine("</td>");
		}

	Hopefully this helps someone who runs into this problem in the
future.

- Chuck

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Thursday, April 25, 2002 10:46 AM
To: ASP+
Subject: [aspx] RenderEndTag, LinkButton and custom CommandEventHandler


	I'm having a problem with getting a LinkButton to actually create an
href.  I'm writing a class that inherits from WebControls.DropDownList, and
essentially tacking some <td>s around a caption and the actual drop down
list.  This class also has an "AllowAddNew" bool property, that when it is
true, should create a link that can be clicked on to raise the OnAddCommand
event of my class.  I'm trying to do all this magic in the RenderEndTag
event:

public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			writer.WriteLine("</select>");
			if (_allowAddNew)
			{
				LinkButton lb = new LinkButton();
				lb.Command += AddCommand;
				lb.Text = _addNewCaption;
				lb.RenderControl(writer);
				if (AddCommand != null)
					writer.Write(" Add Command is not
null.");
			}
			writer.WriteLine("</td>");
		}

	What I am getting is a select box followed by a <a>New...</a>, but
no href, and the " Add Command is not null." statement.  So I know the
AddCommand command event handler has a value and _allowAddNew is true, but
neither .Command nor .Click seem to work for LinkButton or Button controls
in the RenderEndTag event.

- Chuck


  Return to Index