Printing a string from a resource file outside of an asp control - asp.net

I have a resource file in the App_GlobalResources folder and i would like to print some of the string in things like the alt text of images and the title of a href links. I know this can be done using asp.net controls for asp:Image and asp:Hyperlink then print the striong with the <%$ but what I would like to do is use the normal HTML a href and img tags and then print the resource string in that. Can anyone tell me how to accomplish that?

yes , using
<img src="..." runat="server" id="myUniqueID" />
can be accessed in code-behind just like <asp:Image /> would be
most HTML elements with runat attribute and a unique id can be accessed from the viewstate just like an asp: element would be

Use CSS Media Selectors to distinguish between screen CSS and print CSS.
An example for hyperlinks is given in this article. This may not work for all browsers, though.

Related

Add custom markup to .NET Theme .skin file?

I am new to .NET Themes. I would like to display different static HTML content in various places using Themes of my Web Application.
My first attempt was to use the ASP Literal control as follows:
In .aspx page:
<asp:Literal runat="server" SkinID="HomeContentFooter"/>
and in my .skin file:
<asp:Literal runat="server" SkinID="HomeContentFooter">
<div>
<!-- I hoped to put some custom static markup for each theme here -->
</div>
</asp:Literal>
This produces the following error:
The control type 'System.Web.UI.WebControls.Literal' cannot be themed.
So is there another approach for this using themes?
Literal control is rendered as plain text on your html, that is, when you have <asp:Literal ID="id" runat="server">Hello World</asp:Literal> you will get just Hello World on client. Since skin files are used for setting properties of elements and literal does not have style related properties or any properties or tags at all, it makes sense that it cannot be themed.
You can use asp:Panel instead, you customize it in your skin file and it will be rendered as div.

ASP.net HTMLDecode not working for Resource entries

I have a ASP.net MVC project, which utilizes resource entries (.resx) through out the project.
Few the resources fed, have HTML in it
example: Hello <b>World!</b>
With paragraphs href and more. As the resources are stored in an XML, the entries are HTMLEncoded
i.e the above example looks like this
eg: Hello <b>World!</b>
Due to this, wherever the resources are displayed, the HTML formatting does not render, and instead the HTML is displayed as visible text.
I tried to use HttpUtility.HTMLDecode and Server.HTMLDecode, but both wont work.
What is wrong? Any other work around resources?
Both of the following work fine for me:
<%= Resource.MyResource %><br />
<asp:Label runat="server" Text="<%$ Resources:Resource, MyResource %>" /><br />
A Resource entry such as <b>Text</b> is displayed in bold by the browser.
Some Controls do automatic HTML encoding of their inputs. Could that be what's happening for you?

Can I place a comment inside a tag in ASP.NET?

I am looking to display this in my .aspx page, without using special character XML tags, can this be achieved?
<asp:ServerTag Property1="a"
Property2="b"
Property3="c" <%-- Comment why this particular property is necessary --%>
Property4="d" />
However, I am greeted with the error message Server tags cannot contain <% ... %> constructs. If I use an HTML <!-- --> tag, I'm told the server tag is not well formed.
Is there any other syntax to make this possible?
Put server-side comment above your server-side control.
<!-- client-side comment (html) - appears in html source but not rendered on page
<%-- server-side comment - stripped out on server, never sees light of day, browser never knows about it
like this
<%-- Usage:
Property2 is xyz...
Property3 will .. abc. Ignore Property 1 when this is set. etc
--%>
<asp:ServerTag Property1="a"
Property2="b"
Property3="c"
Property4="d" />
It's just like putting source code comments above your functions.
Think "server to server". It will make the difference between your HTML source looking like
cluttered with "pass through" html comment <!--:
<!-- Property usage: abc, def, ...xyz -->
Rendered server control contents.
vs. the cleaner stripped out " <%-- source:
Rendered server control contents.
Less bandwidth with latter too. No extraneous (and confusing to user) comments in HTML source.
It's not possible, no. The server tags need to be well-formed XML and you can't have tags like that in XML. You can put a comment at the top, of course, like so:
<!-- Property2 needed because... -->
<asp:ServerTag Property1="a" Property2="b" Property3="c" />
Not necessarily like that but you may want to consider decorating the property in c# to let the user know its relevance. After that something like resharper (or maybe vs) will give you this information when you try to set it.

How to control usercontrol from javascript

I have an usercontrol with an attribute targetUrl. I add this user control to a page and write targetUrl attribute from this page like below:
<PBG:Modal ID="Modal1"
runat="server"
Height="180"
Width="500"
src="pop_adres_giris.aspx"/>
This worked properly, but I want to change the targetUrl attribute from javascript. And I can't do it. I write code like below, but it didn't work.
var frm = document.getElementById('Modal1');
frm.targetUrl = 'pop_adres_giris.aspx';
How can I do it?
The UserControl object, which generates HTML on the client side, are not accessible as the rich objects which are available when handling server side calls.
Depending what the UserControl is, you will need to use a different method to get it and set the "targetUrl".
In addition, to ease your accessing of elements within the DOM you may want to consider using a library such as jQuery or prototype
Once you have declared your control, for instance, if you were using an asp:Hyperlink control:
<div id="hyperlink_holder">
<asp:Hyperlink ... NavigateUrl="http://someurl" />
</div>
You know that asp:Hyperlink generates html like <a href="http://someurl" ... />
So we can access the element and change the link like:
$('#hyperlink_holder a').attr("href", "http://newurl");
In addition, note that the ID you give an item in ASP.NET is not necessarily the ID which will render in the id element in the HTML; it is instead a concatenation of a number of ids; therefore use selectors based on non runat="server" controls where possible, or pass the ClientID of the UserControl through to the client to use for selection if absolutely necessary.

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