Hello all,
I am creating a custom web control and i would like the literal text between my custom tags to render when the control is loaded. Can someone show me a bare bone code snippet that can do this? You figure ASP.NET would make this easier to do but apparently its not. Here is what I have so far:
CustomControl.ascx.cs (C# source file)
Code:
using System;
using System.Web;
using System.Web.UI;
namespace MyControls {
public class CustomControl : UserControl {
private string _bgcolor;
public string bgcolor{
get { return _bgcolor; }
set { _bgcolor= value; }
}
// Is this right???
protected override void Render(HtmlTextWriter output) {
if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
output.Write("<H2>Your Message: " + ((LiteralControl) Controls[0]).Text + "</H2>");
}
}
}
}
This is CustomControl.ascx
Code:
<%@ Control Language="C#" Inherits="MyControls.CustomControl" Src="CustomControl.ascx.cs" Debug="true" %>
<table>
<tr>
<td bgcolor="<%= bgcolor%>"><!--literal text here--></td>
</tr>
</table>
Finally here is the CustomControlExample.aspx file
Code:
<%@ page language="C#" %>
<%@ register tagprefix="ctrl" tagname="MyControls.CustomControl" src="CustomControl.ascx" %>
<html>
<body>
<ctrl:CustomControl bgcolor="yellow" runat="server">
This literal text should be displayed
</ctrl:CustomControl>
</body>
</html>
|