I'm having a problem while creating dynamically a web page in ASP.NET (C#).
I need to insert multiple form tags in that page, I know that I can't put
more than one with runat="server" attribute and I don't need it.
I want to place them from C# (one for each element I've got to manage), without the runat attribute but the HtmlForm object
that I use for insert the form adds the runat attribute automatically and I can't remove it
(tried with form.Attributes.Remove("runat"))
If I use a simple string like:
"<form id="someID" method="POST" action=""></form>"
and I add it multiple times into my div it works.
The point is that I don't want to insert ALL my HTML objects writing them in a string
and add it with InnerHTML method. I'm looking for an object that manage a Form without
the runat attribute or a way to remove that from HtmlForm.
A user control is probably the cleanest solution but you can add an HtmlGenericControl instead of a HtmlForm object which isn't bound to any specific attributes.
Dim ctrl As New HtmlGenericControl("form")
ctrl.Attributes.Add("id", "someID")
ctrl.Attributes.Add("method", "POST")
ctrl.Attributes.Add("action", "")
OuterDivContainer.Controls.Add(ctrl)
If you want to add controls dynamically to a page and make the web page render them as pure html, instead of rendering asp:Controls you can use the Literal control. Check this link or this one
Related
I have list of images stored in sql database.
i try to add it dynamic at run time .
i use "InnerHtml"
i create dive tag and want to add the image list in the div tag
HTML:
<div runat=server class="ws_images" id="List_Slide">
C#
List_Slide.InnerHtml = "<li><img src=data1/images/31.jpg alt=31 title=31 id=wows1_0/></li>"
can you help me ?
It looks like you are using ASP.NET web forms.
What you can do is drop a asp:PlaceHolder control onto your page then add Literal controls in there that contain your image HTML code.
<asp:PlaceHolder id="ImagePlaceHolder" runat="server"/>
..
..
In code behind:
var literal = new LiteralControl("<li><img src=data1/images/31.jpg alt=31 title=31 id=wows1_0/></li>");
ImagePlaceHolder.Controls.Add(literal);
PlaceHolder does not render any HTML tags so if you want you can put your div tag inside the LiteralControl.
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 have a page that does not have runat="server" set in the <head/> section. I do not have access to modify any of the code in the page.
This page contains a user control which I do have access to. Can I add a <meta/> tag to the head section of the page from the user control? It needs to be server-side so a javascript solution won't work.
One option is to create a Response Filter, and then modify the output before it's sent to the user.
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
You can parse the text in
(this.Page.Controls[0] as LiteralControl).Text
to see where the string <head> starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server".
Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head> element would have been marked as runat="server" so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.
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.
I'm working inside of a Web User Control (.ascx) that is going to be included in a regular web form (.aspx), but I need to be able to dynamically insert code into the head of the document from the User Control. In my Coldfusion days <cfhtmlhead> would do the trick. Is there an equivalent of this in ASP.NET or a similar hack?
To add HTML markup you can do the following:
In your UserControl's code you can access Page.Header, which is itself a control. To that control you can then add new controls:
HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);
To add script markup you don't need access to the head tag at all since ASP.NET has helper methods on the ClientScriptManager that do the work for you:
Here are examples of some code you can also put in your user control's code:
// Register some inline script:
Page.ClientScript.RegisterClientScriptBlock(GetType(), "myAlertScript", "alert('hello!')", true);
// Register a script reference:
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js");
I realize that this is an old question, but this is another example.
Try This:
Page.Header.Controls.Add(
new LiteralControl(
"<script>alert('Literal Added to <Head>.');</script>"
)
);
If you want to add the script at a particular index of the <head> you can use
AddAt(index, new LiteralControl(...)) where index 0 equals the top of the <head>
Also, you need to add runat="server" in your head tag e.g. <head id="head1" runat="server">
this.Page.Header.Controls.Add
By doing this, you are adding controls to the head section. You can add any type of control. If you feel you need to add simple text (or you want to write the tags manually), then look into the LiteralControl class.
There's some guidance on using C# code to modify the page header here. It should work just fine from any server-side code that executes before the page load completes.
A simple e.g.
HtmlHead head = Page.Header;
HtmlTitle title = new HtmlTitle();
title.Text = "Test Page";
head.Controls.Add(title);
HTMLHead reference is in namespace
System.Web.UI.HtmlControls
Override the custom control's Load() method to add the controls or references you need into the page header while the parent .aspx page is being loaded server-side.
I have a simple solution for this. Create a runtime memory cache based on the url of the page (as a key) that holds x information about y (be it a file reference, script text, or class that generates JavaScript) and serialize its data to JSON. Newtonsoft is helpful for instances of any class. In fact, you can use it's output to initialize any new instance of a class based upon given input. In a way, that means you may have your instances of any particular class automatically instantiated despite what user control the instance is on. In the end, you create a simple web form to serve as a script reference and as the final endpoint. It pulls the JavaScript (or what've it) and spits out the client side code you need as a script reference inside the head tag.