When building a custom control, how would you access the content between the opening and closing control tags?
<my:tag runat="server">
<p>...markup</p>...
</my:tag>
I am currently successfully using the Render method to output arbitrary markeup, but cannot seem to find out how to access the contained markup.
Take a look at this.Controls. This article :
http://msdn.microsoft.com/en-us/library/system.web.ui.control.controls(VS.71).aspx states "On an ASP.NET page, when controls are added declaratively between the opening and closing tags of a server control, ASP.NET automatically adds the controls to the containing server control's ControlCollection. "
As far as I understand, if you have
<yourcode:yourcontrol id="asdf" runat="server">
<p id="innerP" runat="server">Text here</p>
</yourcode:yourcontrol>
Then it would be possible to call this.FindControl("innerP").text="Other text here, since the P tag is generated on the server side.
However, if you do not have the runat="server" set on the P element:
<yourcode:yourcontrol id="asdf" runat="server">
<p id="innerP">Text here</p>
</yourcode:yourcontrol>
then you only can only find it through this.controls[0] since all the content will be rendered into a single Literal control.
I think you want to do this:
<my:tag runtat="server">
<p><asp:Label id="markupLabel" runat="server"/></p>
</my:tag>
And from the code-behind
markupLabel.text = "Foo";
If you add an ID to the my:tag tag, you should be able to access the controls inside of it using the .Controls collection of the tag.
Related
When i need to set some value to a Javacript, or any other part of the code, i usually use this:
<script>
<%# SomeFunction() %>
</script>
And this also works for HTML in the document body, like...
<somehtmltag property="<%# SomeFunction2() %>">
And in the code behind i create the function that returns a string, with all the necessary code.
If i add some parameter to a user control like:
<ts:PeopleCard ID="us" runat="server" Visible="<%# IsVisivle() %>" />
It also works, but i try to create the entire user control it does not work.
<%# AddUserControl() %>
Function AddUserControl() as String
Return "<ts:PeopleCard ID=""us"" runat=""server"" Visible=""true"" />"
End Function
I understand that this does not work, because this code must be processed by the server to be converted in the actual code.
The final HTML, it shows:
<ts:PeopleCard ID="us" runat="server" Visible="true" />
when it shouldn't, it should show the processed HTML\css by the server.
So my question is, is it possible to create a control this way? Is it possible to force ASP.NET to "re-process" the page, after I changed its contents in code behind?
I understand there's several other ways to do it. Including, creating the user control in conde behind.
But i need to know, if is possible to do this way...
Usually you have a parent tag that is runat server and you can then add your own object to it.
Dim newTag as New PeopleCard
newTag.Visible = true
pnl.Controls.Add(newTag)
An other option I've done is the past is to add a RenderMethod to a control. Each control have a SetRenderMethodDelegate function and it allows you to write directly to the HtmlTextWriter. This won't create an object for the controls you create yourself.
I learned that in umbraco, we have to use a master template for a custom user control. I have a custom form and a submit button. I created a control that sends an email once the 'submit" button is pressed. the control basically gets all the fields like name, address. It works when I create a new template and insert the macro.
However when I try to use it with a master template, it does not work. The submit button refreshes the page but never goes to my c# part where it sends the mail
and if I do this
<form name="myform" target="_self" method="post" action="index">
it goes to the index page, but no email is sent.
But it works on a clean or new template. Is there a way to use custom control with master template in umbraco 4
EDIT -
This is how I am using macro
<form name="myform" target="_self" method="post" action="index">
<umbraco:Macro Alias="sendMail" runat="server"></umbraco:Macro>
</form>
Looking at the code it looks like you are missing the runat="server" attribute on the form tag.
<form name="myform" runat="server">
<umbraco:Macro Alias="sendMail" runat="server"></umbraco:Macro>
</form>
By using the runat=server attribute the form tag will be rendered with the correct attributes. Note you can only have one runat="server" form on a page.
There's some more information on this concept on W3Schools
I assume you are using ASP.NET WebForms. I have Umbraco sites in which I also use a Master Template and a Macro which contains a form. This should work normally.
What I notice is that you have set the "action" attribute which is ignored. Also the "method" attribute default value is "post" so you don't need to set both these properties.
An ASP.NET Webform is always posting to itself, so you can remove the "target" property here. I'm not sure that removing these "unwanted" attributes will solve your problem.
If not, are you using nested .NET Forms? That is throwing errors and strange behavior as well.
I try to publish a C#/ASP.NET page and receive the following error message:
Control XXX must be placed inside a form tag with runat=server
When I add the <form runat="server"> tag, I receive the following error:
A page can have only one server-side Form tag.
I know that my master page has the runat=server form tag and that the content is encompassed within that tag. I also see that the error appears to be occuring within
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a05ad405\1aeb0277\App_Web_lgjjhruj.3.cs:0
which is I suppose a temporary file?
My question is, has anyone had a similar error and how was it resolved?
Keep Only one in master page
Remove all the form tag from content page,
as well as head and body tag from content page.
and most importantly keep all the controls inside the content place holder that will be rendered inside form tag.
put run at server in all asp controls.
Do you have another form inside of your content page (which uses your master page)? You should have a form which runs at server only in your masterpage. Search your project (ctrl + shift + f) for
Check your MasterPage and make sure all controls with a runat="server" are contained within the <form runat="server"> element.
I am having issues with the output of my C# script embedded in my asp.net code. The output is generated after clicking a submit button for a web form. This web form is at the top of the page. The output, when clicking submit, is currently being placed above the web form which is in turn pushing the web form underneath it. I would like the opposite to happen. I want it to output below my web form. The way I generate output from my script is as follows:
Response.Write("<p>");
foreach(obj in arr){
Response.Write(obj);
}
Response.Write("</p>");
Also if it matters, I initialize the script with runat="server". The script gets called when the user selects "submit" near the web form. Thanks in advance. I've been trying to format this thing for quite some time now.
You would be better off putting a 'literal' object in the place on your page precisely where where you want the result to appear, and then, instead of spitting out HTML with response.write, you assign the desired text to the literal in your code-behind.
Like this:
<html>
<p>
<asp:Literal ID="ltlTest" runat="server"></asp:Literal>
</p>
</html>
and then in your code behind:
ltlTest.Text = "the string you want to show...";
You can include html tags in the string assignment, though generally I try not to.
You've got some choices.
You can make arr a public property, and then use <% foreach (var obj in arr) Response.Write(obj); %> directly in the page markup where you want it.
You can put in an <asp:Literal runat="server" ID="Literal1"> control and then set Literal1.Text = ... in your code. This achieves the same, but with ViewState (so the value is persisted on postbacks).
If you'd like the result to be rendered within <span /> tags, you can use an <asp:Label /> control. This is usually the best choice for displaying messages to the user.
Like the preview box below that take input from the textarea we are typing now.
The requirement is to retrieve HTML saved in database and preview it on the screen. Should I use label or is there any better control around?
I would use the literal control unless you need to do anything "extra" with the html.
I often use a DIV with a runat="server" attribute declared. That way, I have a container to apply CSS classes to, and I know and can control the markup that is being created.
A Literal will work fine if you don't need a container (or you have a container already on the page).
<div class="css-class">
<asp:Literal runat="server" />
</div>
OR
<div runat="server" class="css-class" />
And as Oded said, watch out for XSS by sanitizing your HTML.
If you simply need to output HTML, use a LiteralControl - you simply set its Text property to the HTML you need.
You may want to think about cleaning up the HTML, if it something that is user input, just in case of XSS attacks hiding in your HTML data.