Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
.Net Custom Web Control Howto?
Old 08-05-2004, 03: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
 
 
Register now for full access!
Old 10-27-2004, 07:01 AM
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 198
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.38036 seconds with 12 queries