Render Razor in Html.Raw() (MVC3) - asp.net

Is it possible to render Razor in Html.Raw()? I have a dynamic page being generated that uses the Html.Raw() method to render the page that is created in the controller. Inside this page I have an image tag that should retrieve the image via Url.Action.
var image = string.Format("<img src=\"#Url.Action(\"GetImage\", \"ImageUtility\")?id={0}\" alt=\"{1}\" />", Image.ImageId, Image.Name);
As you can see this would render the following without the razor being rendered:
<img src="#Url.Action("GetImage", "ImageUtility")?id=10000" alt="Image Name" />

Try wrapping Url.Action with parenthesis.
See this answer and #Igor's comment related to.

Related

window without parameters

How can I pass parameters from silverlight(xaml page) to an aspx page without showing them on url link?
if I do this:
HtmlWindow hw = System.Windows.Browser.HtmlPage.PopupWindow(new Uri(path), "rptapp", options);
it will pass my parameters but they will appear in url.
I don't want to encrypt them...I just them not to appear there
Create a form in your page where you host the XAP file of your silverlight application and submit the form to the new url with the post method.
you fill the action attribute with your link and add input elements to it:
<form id="popupform" action="" method="post" target="_blank" style="visibility: collapse" enctype="multipart/form-data" />
Read more here: How to remove all children in HtmlElement with Silverlight/C#

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));

ASP.NET Master Page and User Content Forms

Is there a good way to enable forms that come out of user content (CMS) that is displayed inside the form runat="server" tag? All sorts of services provide forms for users to paste into their website, which break if the content ends up inside a .net form. It seems to me that there must be a good way around this, but I can't seem to find any solutions.
The one solution I've found is to put them in an IFRAME. So instead of embedding your form inside the page:
<FORM action="http://www.example.com/target.html">
.. form content
</FORM>
create a new ASPX file like this:
<FORM action="http://www.example.com/target.html" target="_top">
.. form content
</FORM>
Then in your original page, you'll refer to it via an IFRAME:
<IFRAME src='myform.aspx'></IFRAME>
Some things you'll need to do:
Make sure that you do not include any <FORM runat="server"> inside the new ASPX file.
Adjust the size of the IFRAME and the ASPX file to be exactly the same
For the FORM in the new ASPX files, you probably want to set target='_top' to ensure when a user fills in the form, the result is in the main window rather than inside the IFRAME.

How do I set the target frame for form submission in ASP.NET?

I have an asp.net page in an iframe where all links target _blank
<base target="_blank" />
But I want the form on it to submit to _self (i.e. the iframe where the page is located) when the one button is clicked. The form is an <asp:Panel> with an <asp:Button> control for submitting it.
Where can I set the target for this form? Since there isn't a <form> tag or an <input> tag in the file (ASP.NET makes them when it renders the page), I don't know how to change the target to override my <base> tag.
I'm not sure if I understood you right, but you can set the target in the form tag, like this:
<form method=post action="Page.aspx" target="_self">
I just found this post on the asp.net forums by mokeefe that changes the target by javascript.
I just put it in my page and tried it. I had to make these modifications:
1. I'm using asp tags only, so I have <asp:Button> not <input type="button"> and my onclick has to be the server-side method I'm calling on submission. Therefore I put this new javascript in OnClientClick:
<asp:Button ID="cmdEmailSearch" runat="server" Text="Search"
OnClick="cmdEmailSearch_Click"
OnClientClick="javascript:pageSubmit()"/>
2. I removed the myForm.submit() since ASP.NET renders the page putting the WebForm_DoPostBackWithOptions() javascript in the button's onclick right after it puts my pageSubmit()
<script type="text/javascript">
function pageSubmit(){
// where form1 is the Parent Form Id
var myForm = document.getElementById('form1');
myForm.target = '_self';
}// end function
</script>

ASP.NET Img tag

I have a problem with how ASP.Net generates the img tag.
I have a server control like this:
<asp:Image runat="server" ID="someWarning" ImageUrl="~/images/warning.gif" AlternateText="Warning" />
I expect it to generate this:
<img id="ctl00_ContentPlaceHolder1_ctl00_someWarning" src="../images/warning.gif" />
but instead it generates this:
<img alt="" src="/Image.ashx;img=%2fimages%2fwarning.gif"</img>
This give me errors when I execute the following js:
document.getElementById('ctl00_ContentPlaceHolder1_someWarning')
Any idea why it won't generate the expected html?
Looks like it's trying to use a custom handler (ashx) to deliver the image. Do you have any additional modules that may be overriding the default behaviour of the asp:Image?
Your JavaScript won't work because the image tag has not been given an ID in the HTML that was generated.
You can get the actual ID that is generated by using ClientID. I use this to get the ID of a control for use in JavaScript using syntax similar to the following:
document.getElementById('<%=ddlCountry.ClientID%>').style.display = "block";
However you can also use it in your code-behind to get the same thing.

Resources