Setting Page Title from an HtmlHelper class - asp.net

I have an HTML Helper that essentially renders static content read from HTML files (inside of a vdir). There are cases when the HTML file has a title element defined and in such cases, the current page should use the given title. The content inside of the body should be rendered where the Helper class is referenced in the View.
This is how I call the helper.
<%=Html.StaticContent("staticcontent.htm",
new List<StaticContentTag>()
{
new StaticContentTag()
{TagKey=ReplaceTags.MarketName,
TagValue = "Austin"}
}, Model, true) %>
I'm passing in the ViewModel so that I can set the title and the last parameter is a flag that says whether to force the title or not.
The head has the title defined like this.
<title><%=Model.Title%></title>
I know what I'm doing wrong here by referencing the Model.Title element before even calling the helper. Any ideas on how I can work around this?

i believe ur title tag is rendered before u call the html helper in ur view. the purpose of helpers is to render html tags where they are called not to change contents of already rendered tags that can be done through javascript. however i would not use all that new keywords in my view. rather i would make a view model containing all required information for the view and then i would have no problem writing statement
<title><%=Model.title%></title>

Related

How can I generate the html code for a drop-down inside a controller in MVC?

I have a javascript function, part of which has a section that accepts HTML code. Right now, I have the following line in that javascript code:
html: `#Html.Action("Position","Admin")`
My issue is that my Position view has only one line in it:
#Html.DropDownList("PositionId",null,htmlAttributes: new {#class = "form-control"})
However, I feel like it's a waste to have an entire View created with just one line. I'm only using the view because I need to generate the HTML for the DropDownList via Razor to pass to my function.
How can I generate the resulting HTML code in my controller without having to create a view?

Validation while rendering multiple partial views

I have multiple views/models that are being looped into the main view. I load them via #Html.Partial()...These views/models are basically form elements with certain properties...Unfortunately I soon found out that only the first of each type of view/model is validated. I tried moving the fields around and only the first of each kind would validate.
My partial views look something like this:
#Html.DropDownListFor(model=>model.dropdownVal,Model.SelectItems,new { id=Model.FieldID, Name = Model.FieldID })
I looked at the HTML rendered, and it seems that the validation tags like "data-val" are not applied...
Any ideas would be greatly appreciated!
Add the following at the top of your partials to trick ASP.NET MVC into thinking that the helpers are used inside a form and generate the proper data-val attributes:
#{
this.ViewContext.FormContext = new FormContext();
}
Basically the Html.* helpers are generating data-val clients side validation attributes only if they are placed inside an Html.BeginForm. Except that in your case I guess that this Html.BeginForm is in your parent view and not inside the partial, so the #Html.DropDownListFor doesn't emit any validation attributes. By setting the current FormContext to a new instance in the partial as shown previously, the helper will generate the proper client side validation attributes on the corresponding input field.

Loop through child items and get image field for showing on parent's view/template

I have a parent content type RetailFont, it has child types, and one is called FontWeight.
Fontweight has an image field called WeightImage.
I have successfully added a new view and template for RetailFont (called FontWeightView). I would like to loop through and show some of the fields for all the WeightImage items inside it, such as its title and ImageField.
I tried copying folder_summary_view.pt contents to my template file but it generates errors.
Does it need something in my FontWeightView.py file to work? http://www.pastie.org/3286449
test() is a deprecated page template method and should no longer be used.
You can work around this with the logic:
<tal:something condition="python:somecond and ifistrue or ifisfalse" />

Wrapper for custom Content Part - Orchard CMS

Using the Orchard admin I created a new Content Part called 'Spotlight Wrapper' with 3 HTML fields. I then created a Content Type Called 'Template 1' and assigned 'Spotlight Wrapper' to it. I then created a new 'Template 1' content item called 'Home Page'. I then created a file called Fileds_Contrib.Html-Spotlight Wrapper.cshtml to wrap each HTML field in the 'Spotlight Wrapper' with an and this is working. I have now added:
<Place Parts_SpotlightWrapper="Content:before;Wrapper=Parts_SpotlightWrapper" />
And created :
Views\Parts.SpotlightWrapper.cshtml
in an attempt to wrap the entire 'Spotlight Wrapper' Content Part in a but cannot seem to get it to work?
You declared a wrapper which I guess would lead to circular reference, as you try to wrap the Parts_SpotlightWrapper shape with itself. Wrappers are just separate pieces of Razor (cshtml) code that act as a parent for a given shape.
To achieve the behavior you want you should create a separate .cshtml file (eg. MyWrapper.cshtml) containing the necessary wrapper HTML code and attach it to your existing part like this:
<Place Parts_SpotlightWrapper="Content:before;Wrapper=MyWrapper" />
The wrapper code could look eg. like this:
<ul>
#Display(Model.Child)
</ul>
Btw - Try to look how it's done in Orchard.Widgets. There are two wrappers Widget.Wrapper and Widget.ControlWrapper that wrap the Widget shape. Declarations of those are not inside the Placement.info file (as you did), but hardcoded in Shapes.cs shape definition, though the final effect is perfectly the same. The technique with the Placement.info was just introduced later as a shortcut.
HTH

How can you move ASP.Net controls to different places on the Web form at runtime?

Is there an accepted way to "move" a control.
My client wants to place a certain chunk of markup (representing some visual element) in one of several different places on the page. The locations are different to the point that I can't effect the change on CSS along (by floating it or something).
I considered just putting the control in multiple spots with Visible set to "false," then displaying the one in the place they wanted for that particular page.
However, the code for this control is not trivial -- there's a couple template sections, for instance. Having to dupe this in multiple places would get unwieldy. Also, I don't want to have to work with this control strictly from the code-behind for the same reason.
So, I'd like to put it in one place on the Web form, the move it around based on where I want it. Could I put Placeholders in different spots, have the control in one spot, then remove and add it to the right spot? I suspect this would work.
Does someone have a better idea? Is there a best practice for this?
I'd recommend using a placeholder control, moving your markup into a separate user control, then loading this at runtime and adding it to the relevant placeholder.
Eg.
// Load a user control
MyControl userCtrl = (MyControl) LoadControl("~/Controls/MyControl.ascx");
// Or create an instance of your control
SubclassedControl subclassedCtrl = new SubclassedControl();
// Do stuff with controls here
userCtrl.LoadData();
subclassedCtrl.Text = "Hello World";
// Check which placeholder to add controls to
PlaceHolder placeHolder = (foo=="bar") ? placeHolder1 : placeHolder2;
// Add the controls
placeHolder.Controls.Add(userCtrl);
placeHolder.Controls.Add(subclassedCtrl);
This will avoid cluttering up your page with unnecessary markup, and loading it at runtime will also avoid unnecessary confusion later, when another developer looks at the code and can't immediately see why a control is in one place in the markup, but renders on a completely different part of the page.
An alternative (and one I've seen done many times before) is through javascript and the DOM. Render your control inside a hidden div tag. So you would render your content here:
<div id='rendercontent' style='display:none'>
.. control here ..
</div>
Then, lets say you wanted to move it all here (the span tag is inside because that's what we're going to replace):
<div id='newlocation1'><span></span></div>
You would define the following javascript:
<script language="JavaScript">
function replaceNode(newElementID, targetElementID)
{
var targetElement=document.getElementById(targetElementID);
var newElement=document.getElementById(newElementID);
targetElement.replaceChild(newElement, targetElement.firstChild);
}
</script>
And when you want to move the content to the new location, call:
<script language="JavaScript">
replaceNode('rendercontent','newlocation1');
</script>
Do Web Parts do what you want to do?
Or, you can change the parent programmatically of your controls to move them into a separate area.
You can override the Render method and place the controls wherever you want in the html.
You only need to add controls to the Controls collection that must interact on the server. The rest of your HTML can just be written to the response stream. If you override Render you can create the html anyway you see fit, placing the controls in any order.
Below is an example of how to write out your html.
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.RenderBeginTag(TagKey);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
_control.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
}
You could always put panels in the pre-defined locations and add the control to the specific panel at runtime.. Here's an example adding a label (the label could be replaced with any control).
Dim lblDisplay As Label = New Label()
lblDisplay.ID = "myLabel"
lblDisplay.Text = "Some Text"
pnlDisplay.Controls.Add(lblDisplay)
As far as...
"Also, I don't want to have to work
with this control strictly from the
code-behind for the same reason."
I think you're going to have to do most of your work in the code behind.
PS.. a good example of the whole usercontrol setup can be downloaded here..
http://www.asp.net/downloads/starter-kits/time-tracker/

Resources