Reply
.Net Custom Web Control Howto?
Old 08-05-2004, 02:10 PM .Net Custom Web Control Howto?
Junior Talker

Posts: 1
Trades: 0
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>
rparnaso is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-27-2004, 06:01 AM
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 199
Location: High Wycombe, Buckinghamshire, London
Trades: 0
um - never really tackled custom controls, but if you getting an error, change CustomControlEmail.aspx to this:


Code:
<%@ Page Language="C#" Register tagprefix="ctrl" tagname="MyControls.CustomControl" src="CustomControl.acsx" Debug="True" Trace="True" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.IO" %>
<html>
<body>
   <ctrl:CustomControl bgcolor="yellow" runat="server">
      This literal text should be displayed
   </ctrl:CustomControl>
</body>
</html>
dunno if that will work but we can all try
__________________
[ Insert witty, yet highly intelligent signature here ]
sdcdesign.co.uk is offline
Reply With Quote
View Public Profile Visit sdcdesign.co.uk's homepage!
 
Reply     « Reply to .Net Custom Web Control Howto?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.09412 seconds with 13 queries