Forum Moderators: open

Message Too Old, No Replies

user controls with inner text?

how do I make my own tags

         

httpwebwitch

4:31 pm on Jun 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My successful experiments with User Controls have all been done like this:

<myid:tagname
runat="server"
myfile="three.xml"
showlines="false"
/>

Now I want to achieve something more like this:

<myid:bigbox>
my text my text my text my text
</myid:bigbox>

Are User Controls capable of that? If so, is anyone here willing to show me how?

Jimmy Turnip

3:45 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



If in your first usercontrol you have something like

<myid:tagname
runat="server"
myfile="three.xml"
showlines="false"
innertext="blah blah blah"
/>

And then in the UserControl codebehind you declare:
Public innertext As String

Then on the UserControl front end:
<% Response.Write(innertext) %>

This will have the same affect. Not sure if it's the best way, but it'd work.

httpwebwitch

3:55 pm on Jun 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've figured out the whole attribute thing,

I was wondering if I can wrap some CDATA or HTML inside a pair of custom tags that will perform an action on the inner text

<myid:replacespaces runat="server">
html here
html here
text here
</myid:replacespaces>

httpwebwitch

3:57 pm on Jun 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't be shocked if my idea isn't possible, but it's so darned frustrating with the opacity of documentation available for User Controls (I've been scouring the www and MSDN for days)

Jimmy Turnip

4:18 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



seems to just ignore everything between the usercontrol tags. I think it's because everything in a usercontrol needs to be encompassed together. There must be another solution, like assigning a header and footer attribute. What are you trying to do exactly?

TheNige

8:04 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Do a search on "asp.net user control public properties" on a search engine. Many examples will show you exactly what you want to do.

httpwebwitch

2:52 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



turnip:
What are you trying to do exactly?

I'm trying to find a way to wrap custom tags around HTML content to parse/alter/change that content.

<myid:customreplace>
hello world
<p>hello world</p>
<i>hello world</i>
</myid:customreplace>

One thing I'm trying to do immediately is to hide elements from the menu if the user hasn't logged in. Another is to take a long passage of HTML and turn it into a Flash movie or a graphic image.

I had a thought this weekend: If I can't use a user control, what if my program used output buffering and grabbed the chunk of code to manipulate it before it's sent?

1) grab the output buffer
2) extract the HTML bits using regex [code]/<mytag>.*?</mytag>/[code]
3) do the manipulation on that and replace it
4) then send the results to the client

I'm familiar with output buffering in PHP, but I've never tried it in ASP.NET. can I take an output buffer and apply a replacement to it?

httpwebwitch

4:43 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



TheNige:
Do a search...

I'm finding nothing that uses anything like InnerHTML - it's nothing but attributes

TheNige

8:29 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



use an Asp.Net control that uses the innertext or text property already such as the label control.

<asp:label runat=server id=lblLabel>My Text</label>

you can then access the text in the codebehind with "lblLabel.Text". You could then process this on the page load to do what ever you want.

Or, create your own control and inherit from the label control and make it do the transformation automatically.

httpwebwitch

2:00 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that last suggestion sounds appealing

how do I do that?

edicius

6:15 pm on Jun 14, 2005 (gmt 0)

10+ Year Member



I guess I'd ask why you would need a custom user control in this case - you may be better served by System.Web.UI.WebControls.Literal control or a System.Web.UI.WebControls.Label control.

The major difference between the two is that a Literal control does not include all the style-based properties a great. If you have more general styles to apply to ALL the text in the control - use a label.Label has. If you know the specific HTML you want to put in (Paragraph/Bold/etc. tags) - the Literal will probably work

You can programmatically control the text displayed in either control by setting the Text property.

Example:


<asp:Literal id="myText_lit" runat="server" Text="my text<br/><b>my text</b><br/>my text<hr/>my text"/>

Custom User Controls are typically used when you find yourself needing to reuse the same formatting/structure on a page multiple times (and typically other code-behind programming logic that affects the display). It may be overkill if you're just trying to format text. Implementation of this route would be similar however - just create your User Control with a literal control/label control/etc. - fill the text properties as you feel fit; and include the call to it in the parent in both the HTML and code behind (if applicable).

edicius

6:25 pm on Jun 14, 2005 (gmt 0)

10+ Year Member



httpwebwitch has a good suggestion too (if you need to create your own control/inherit from another web control). To inherit just change the underlying implementation type of your custom user control:


namespace System.Web.UI.WebControls.Custom
{
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public abstract class WidgetLabel : Label
{
///<summary>Implementation here</summary>
}
}

sqlgod

9:52 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Create a new control that inherits from the label webcontrol