Using an explicit localization expression for a page title? - c#

I am trying to get an explicit localization expression for a page title, but can't seem to figure out how, and a google search comes up with nothing.
With a control, it's nice and easy:
<asp:literal runat="server" text="<%$ Resources:MyResource, StringId %>" />
But how does one do this for the page title? I've tried specifying it in the page directive, but that of course doesn't work:
<%# Page Title="<%$ Resources:MyResource, StringId %>" ...
Is there a way to do this? Or is it simply not possible?

You can also use either
Page.Title = Resources.MyResource.StringId;
or
<title><%= Resources.MyResource.StringId %></title>

simply in the page load event write
Page.Title="any String u want";

Related

How to update Html control without runat="server"

I have an Html image which I need to update dynamically but without runat="server" my code behind cannot see it.
<img name="MDI" id="detailimg" src="" alt="blankimage.jpg" />
I can access it using Page.Request.Form["MDI"]; but not update it
I need to load the src image at run time so can someone please tell me how I can update the control in my code.
Do with inline syntax like below
<img name="MDI" id="detailimg" src="<%= getImageSource() %>" alt="blankimage.jpg" />
In codebehind
public string getImageSource()
{
return "urlpath/img.jpeg";
}
More about Inline Syntax
If you want to update via code behind, you have to set runat="server"
If inline is fine, use src="<%= something%>
Otherwise use javascript for example
as you know if you want to access an element/control from code behind,you should set runat="server" but there is 2 other ways to set properties from code behind
use inline syntax as other guys shows (<%= YourVoidToGetImgSrc %>
use Ajax and get value from code behind in your html page.

Embedding display expression inside asp.net tag

I have a class that just have defined url constants, let's say it is called Urls. I am trying to access a constant in that class inside of a a LoginStatus tag as follows:
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="Redirect" LogoutPageUrl= <%= Urls.logout %> CssClass="myButton" />
and obviously it isn't working. I know I can hardcode the string inside of the field, but I am trying to avoid that if possible. Thanks for the insight!
Try this
LogoutPageUrl="<%# Urls.logout %>"
The # is used to "bind" values to server side properties whereas the = is the same as a Response.Write() which in this case will just write some text to the markup

Output static property directly on .aspx page

I want to output something like this on an aspx page (not codebehind):
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
Where Fields.FirstName is a static class. How do I do this? I'm getting an error saying "The name 'Fields' does not exist in the current context". What am I missing? Do I have to include something on the .aspx page?
You could try this:
<input type="text" value="<%=Fields.FirstName %>" id="txt1" />
However, bear in mind that it is no longer a server control.
It is possible to use the <%# Fields.FirstName %> notation in server controls, however they will only be populated when you call DataBind from the code-behind. It is quite custom to use single quotes in the outer scope since double quotes are often needed in the inner scope, like here:
<input type="text" value='<%=Fields["FirstName"] %>' id="txt1" />
But if no quotes are needed it should also work as you described:
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
As long as you call txt1.DataBind() somewhere in the code behind.
See also this question for more info.
Use the full class name (including all nested namespaces) and an = sign, you are not databinding (denoted by the # sign). I commonly do this...
<%=Namespace.MyStrings.MyConstantString%>
Also, depending on how your page is setup, you might have to use single quotes areound the response write brackets....
<asp:TextBox ID="..." runat="server" Text='<%=Namespace.MyStrings.MyConstantString%>'></asp:TextBox>
UPDATE:
Super hacky, but I got it to work...
<supr:SuprTextBox ID="txt" runat="server" ClientIDMode="Static"></supr:SuprTextBox>
<div id="preload" style="display:none;"><%=Supr.Strings.ASSET_CONTROL_LOCATION%></div>
<script type="text/javascript">
$(function () {
$('#txt').val($('#preload').html());
});
</script>
Had to redeem myself after the <%= syntax not working.
You would need to do this in the code behind or in a code snippet on the aspx page. You cannot nest asp tags (<%# %>) cannot be nested in the asp:text element.

Disable hyperlink in .NET

Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>

ASP.NET TextBox - is it possible to initialize text attribute with in line code <% %>

I need to initialize the text attribute of the text box element with a property from some where else when actually I can simply do this from code but it will be much more convenient if it possible to do it like this:
<asp:TextBox runat="server" Text="<%= new ContextItem("title").Value %>" />
Unfortunately the above can't be done..
The issue is that this text box element repeats it self several times in the page and my question is:
Are there any suggestions how to make it cleaner then to write it again and again in the code behind?
Thank,
Adler
OK so the basic problem here is that if you use an inline expression you can NOT use it to set a property of a server-side control outside of a binding context (using a binding expression). I have inferred that this is probably because of the timing of the evaluation of these inline expressions. You can, however, render client-side markup in this way. If you want to keep the functionality purely in your aspx file, this is the way to do it.
Edit: Based on input from Justin Keyes, it appears it IS possible to use a binding expression to set the property. You need to manually invoke Page.DataBind() to trigger the textbox to evaluate the expression (see answer below).
For instance this:
<asp:Label ID="lbl" runat="server" Text="<%= Now.ToShortDateString() %>" />
Will produce this output:
<%= Now.ToShortDateString() %>
On the other hand this:
<%= "<span>" & Now.ToShortDateString() & "</span>"%>
Will produce this output:
7/27/2011
The "normal" way to solve this problem is just to set the Label.Text properties in a Page.Load event handler or another appropriate event handler depending on your needs, as below. This is the way I believe most people would prefer to do it, and is most easily understandable in my opinion.
Markup:
<asp:Label ID="lbl" runat="server" />
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lbl.Text = Now.ToShortDateString()
End Sub
Option 1: don't use server controls
If you aren't accessing the value on the server, just use plain HTML instead of an ASP.NET server control:
<input ID="Textbox1" Type="Text"
Value='<%= new ContextItem("title").Value %>' />
Option 2: use Page.DataBind()
If you change your code to use <%# instead of <%= (as below) and call Page.DataBind(), it will work (I've tested it). Change your markup to this:
<asp:TextBox runat="server" Text="<%# new ContextItem("title").Value %>" />
And in your logic, call Page.DataBind() in the Load event like this:
protected void Page_Load(object sender, EventArgs e) {
Page.DataBind();
}
Even though the TextBox is not contained in a typical "data bound" control such as a Repeater or GridView, calling DataBind() on a control will force it to evaluate <%# ... %> statements.
The Moof's comment (below) is correct. This post also mentions Page.DataBind().
You can set the text on a page in a similar way.
<asp:TextBox id="TextBox1" runat="server" Text='<%#GetValue('Title)%>' />
But in order for this to work, you will need to DataBind the control on Page_Load. For multiple TextBox controls you could just loop through each and databind them so that you do not have to hard code the databinding of each.
I am not sure what your ContextItem is though, so you would have to modify my code.
The short answer is NO, you can only use this kind of code with databindings, that means inside a GridView for example. But you can use this in the head section.
I use it to prefix my urls sometimes with something predefined. Example
<script src="<%=Utils.GetGeneralPrefix()%>/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
In that case it works.
Hope it helps.
90% of the time when I try this I have to use single quotes ('') instead of double quotes ("") around the <%%>. Give that a try before you spend too much time on anything else.

Categories

Resources