Earl777
1/19/2003 11:35:00 PM
Ok, I answered all my own questions! Yeah!
Here is the control code to make a custom control that can be adjusted to
handle any event on a client tag. I my case, I have it handle the onclick
event of a Table <TR> tag. It also uses the 'CommandEventHandler' instead of
the default 'EventHander' so that I can set the control properties
'CommandName' and 'CommandArgs' and retrieve them in the event handler. I
have yet to and the fancy stuff like appearance control but that is
trivial...
If anyone knows how I can make this code better, please let me know. This is
my first attempt at creating a custom client event handler control.
Earl
earlt777@hotmail.com
/****************CONTROL CODE***************************/
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace csCustomControlBubbleTest
{
[DefaultProperty("Text"),
ToolboxData("<{0}:CreateCustomLinkButton
runat=server></{0}:CreateCustomLinkButton>")]
public class aCreateCustomTableRowButton :
System.Web.UI.WebControls.WebControl, IPostBackEventHandler,
INamingContainer
{
[Bindable(true),
Category("Behavior"),
DefaultValue("")]
public string Text
{
get
{
string s = (string)ViewState["Text"];
return ((s==null)?String.Empty:s);
}
set
{
ViewState["Text"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue("")]
public string CommandArgs
{
get
{
string s = (string)ViewState["CommandArgs"];
return ((s==null)?String.Empty:s);
}
set
{
ViewState["CommandArgs"] = value;
}
}
[Bindable(true),
Category("Behavior"),
DefaultValue("")]
public string CommandName
{
get
{
string s = (string)ViewState["CommandName"];
return ((s==null)?String.Empty:s);
}
set
{
ViewState["CommandName"] = value;
}
}
public event System.Web.UI.WebControls.CommandEventHandler Click;
void IPostBackEventHandler.RaisePostBackEvent(string s)
{
CommandEventArgs e = new CommandEventArgs(CommandName,CommandArgs);
OnClick(e);
}
protected virtual void OnClick(CommandEventArgs e)
{
if (Click!=null)
Click(this, e);
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute("Width","400");
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.AddAttribute("bgColor","dimgray");
writer.AddAttribute("OnMouseOver","style.cursor='hand';style.backgroundColor
='Red';");
writer.AddAttribute("OnMouseOut","style.cursor='default';style.backgroundCol
or='dimgray';");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.GetPostBackClientHyperlink(this, String.Empty));
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(Text);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
}
/***************Event Handler************************/
private void aCreateCustomTableRowButton1_Click(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Label1.Text = "This is a test" + e.CommandName;
}
this.aCreateCustomTableRowButton1.Click += new
System.Web.UI.WebControls.CommandEventHandler(this.aCreateCustomTableRowButt
on1_Click);
"EarlT777" <earlt777@hotmail.com> wrote in message
news:uYNfXF2vCHA.1872@TK2MSFTNGP12...
> Objective:
> To create a Server Side OnClick event for a Client (Table Row) OnClick
> event.
>
> Notes
> I found several ways of performing this action but need help to get it
right
>
> 1. First of all, the code below works but I must write out the
__DoPostEvent
> function and related hidden fields myself and have hard coded the form
name
> into the javascript code. (very bad). What is the correct way to do this
and
> is there a .NET method that does this for me?
>
> 2. How can I impliment System.Web.UI.WebControls.CommandEventHandler and
> CommandEventArgs so that I can set the CommandName and CommandArguments
for
> the control and use them in to store information related to the control.
>
> 3. Is there an artical or other resource that deals specifially with this
> issue. I have 2 books on custom controls and niether addresses this issue.
>
> Thanks for the help
>
> Earl
>
>
> using System;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.ComponentModel;
> using System.Collections.Specialized;
>
> namespace csCustomControlBubbleTest
> {
> public class PostBackControl : System.Web.UI.WebControls.WebControl,
> IPostBackEventHandler
> {
>
> public System.EventHandler Click;
>
> public void RaisePostBackEvent(string s)
> {
> OnClick(EventArgs.Empty);
> }
>
> public void OnClick(EventArgs e)
> {
> if (Click != null)
> Click(this, e);
> }
>
> protected override void Render(HtmlTextWriter output)
> {
>
> output.Write("\n<input type=\"hidden\" name=\"__EVENTTARGET\"
value=\"\"
> />");
> output.Write("\n<input type=\"hidden\" name=\"__EVENTARGUMENT\"
> value=\"\" />");
> output.Write("\n<script language=\"javascript\">\n<!--\nfunction
> __doPostBack(eventTarget, eventArgument) \n{\nvar theform =
> document.WebForm4;\ntheform.__EVENTTARGET.value =
> eventTarget;\ntheform.__EVENTARGUMENT.value =
> eventArgument;\ntheform.submit();\n}//-->\n</script>\n");
> output.AddAttribute("Width","400");
> output.RenderBeginTag(HtmlTextWriterTag.Table);
> output.AddAttribute("OnClick","javascript:__doPostBack('"+
this.ClientID
> +"','')");
>
>
output.AddAttribute("OnMouseOver","style.cursor='hand';style.backgroundColor
> ='Red';");
>
>
output.AddAttribute("OnMouseOut","style.cursor='default';style.backgroundCol
> or='dimgray';");
>
> output.AddAttribute("bgColor","dimgray");
> output.RenderBeginTag(HtmlTextWriterTag.Tr);
> output.RenderBeginTag(HtmlTextWriterTag.Td);
> output.Write("Cell Contents");
> output.RenderEndTag();
> output.RenderEndTag();
> output.RenderEndTag();
> }
> }
> }
>
>
>