I'm working on a project where I'm using the <%= getString("key")%> to dynamically get the appropriate text.
this works great when i use it in simple p tags, but i can't find a way to do this with controls like Button/Label etc.
Is there any way, other than calling
Mybutton.Text = getstring("key");
to dynamically add the text?
The idea is that getString retrieves af language code, and depending on that code gets a string in the appropiate language.
I've been looking around, but all i come across is using the embedded code tags directly in the aspx pages, which wont cut it for buttontext.
If you can use DataBinding instead of the <%= operator, you can use:
<asp:Button ID="MyButton" Text='<%# getstring("key") %>' />
This is a good explanation of why <%= won't work in this context.
Related
When i need to set some value to a Javacript, or any other part of the code, i usually use this:
<script>
<%# SomeFunction() %>
</script>
And this also works for HTML in the document body, like...
<somehtmltag property="<%# SomeFunction2() %>">
And in the code behind i create the function that returns a string, with all the necessary code.
If i add some parameter to a user control like:
<ts:PeopleCard ID="us" runat="server" Visible="<%# IsVisivle() %>" />
It also works, but i try to create the entire user control it does not work.
<%# AddUserControl() %>
Function AddUserControl() as String
Return "<ts:PeopleCard ID=""us"" runat=""server"" Visible=""true"" />"
End Function
I understand that this does not work, because this code must be processed by the server to be converted in the actual code.
The final HTML, it shows:
<ts:PeopleCard ID="us" runat="server" Visible="true" />
when it shouldn't, it should show the processed HTML\css by the server.
So my question is, is it possible to create a control this way? Is it possible to force ASP.NET to "re-process" the page, after I changed its contents in code behind?
I understand there's several other ways to do it. Including, creating the user control in conde behind.
But i need to know, if is possible to do this way...
Usually you have a parent tag that is runat server and you can then add your own object to it.
Dim newTag as New PeopleCard
newTag.Visible = true
pnl.Controls.Add(newTag)
An other option I've done is the past is to add a RenderMethod to a control. Each control have a SetRenderMethodDelegate function and it allows you to write directly to the HtmlTextWriter. This won't create an object for the controls you create yourself.
Alright, I have an embarrassingly simple question that hopefully has an embarrassingly simple answer. I am trying to get an asp:ImageButton to display an alert message that contains variables from my codebehind. Here's the overly simplified version of the code:
public string AlertMe
{
get
{
return "alert('hi');";
}
}
Now when I try and access it this way:
<asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" OnClientClick='<%=AlertMe%>'/>
I see a postback, but no alert message, as if it can't access my property at all. Meanwhile, these 2 lines:
<%=AlertMe%><br />
<a onclick="<%=AlertMe%>">click this</a>
both work fine and dandy. (The first displays the code for the javascript alert, and the second fires the alert with no issues.)
So my big question is: why does the OnClientClick event for an asp control fail to register the alert? Is there an obvious flaw I'm missing, or do I really need to register the entire event from code-behind?
You can't use the <%= ... %> syntax to set attributes of a Server Control. To set dynamic attributes on Server Controls, you have to set it in code.
this.btn.OnClientClick = this.AlertMe;
This is because <%= ... %> is a shortcut for Response.Write(). So, it isn't called until after the aspx has been parsed, evaluated, and rendering has begun. By then it's too late.
As an alternative to setting the value in the code behind, you could use this quasi-declarative technique:
<asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" />
<%
btn.OnClientClick = <%=AlertMe%>;
%>
There are other ways to get around it. This answer to a similar question discusses several approaches you can take. I can't find any "official" reference (eg, msdn) that mentions this specifically, but I didn't look that hard. Here are a couple of blog posts that discuss the issue as well:
Blog post by Jim Black: Server tags cannot contain <% ... %> constructs
Blog post at weblogs.asp.net: Use ExpressionBuilder To Avoid “Server tags cannot contain <% … %> constructs”
Or google it with bing for more fun reading.
For anyone else encountering this issue, I ran across this blog from way back when:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
I found the sections about Expression Builders and ViewState concerns to be particularly informative.
I think its a quoting problem. Try
OnClientClick="<%=AlertMe%>"
I'm localizing an ASP.NET web site. Usually to localize text in an .aspx page I just use
<%= Resources.ResourceFile.ResourceName %>
For asp.net controls, this won't work. I have to use the syntax
<%$ Resources:ResourceFile, ResourceName %>
However, if I have a button and localize the Text property that way, but add any additional characters after it, the localization breaks and it shows as plaintext.
So Text="<%$ Resources:ResourceFile, ResourceName %> »" displays as
<%$ Resources:ResourceFile, ResourceName %> »
I'm sure there is a valid reason for this, I just can't find the explanation on MSDN on how the Text property evaluates this. I'm not even 100% sure on what the <%$ actually does.
What's happening is that ASP.net is invoking an Expression Builder. What effectively happens here is that rather than the ASP.net compiler translating your:
<asp:AControlWithATextProperty runat="server" Text="Some Text">
to:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = "Some Text";
When it transforms the markup in the .aspx file into a .cs file combined with the code-behind, it actually does something similar to this:
<asp:AControlWithATextProperty runat="server" Text="<%$ Resources:ResourceFile, ResourceName %>">
Becomes:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename");
It would seem that the asp.net compiler can't handle concatenating the content of the <%$ %> tags with any further text in the property from markup. Either a bug, or by design. i.e. You don't end up with ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename") + "»".
You can read more about the ResourceExpressionBuilder on msdn, ExpressionBuilder in general, or if you really want to; an implementation of one for localisation (database backed, hence the fact that I didn't use the ResourceExpressionBuilder) on my blog (3 parts).
Like the preview box below that take input from the textarea we are typing now.
The requirement is to retrieve HTML saved in database and preview it on the screen. Should I use label or is there any better control around?
I would use the literal control unless you need to do anything "extra" with the html.
I often use a DIV with a runat="server" attribute declared. That way, I have a container to apply CSS classes to, and I know and can control the markup that is being created.
A Literal will work fine if you don't need a container (or you have a container already on the page).
<div class="css-class">
<asp:Literal runat="server" />
</div>
OR
<div runat="server" class="css-class" />
And as Oded said, watch out for XSS by sanitizing your HTML.
If you simply need to output HTML, use a LiteralControl - you simply set its Text property to the HTML you need.
You may want to think about cleaning up the HTML, if it something that is user input, just in case of XSS attacks hiding in your HTML data.
I am trying to create an ASP.net server control for displaying formatted code, using this library: http://www.manoli.net/csharpformat/
Here's the trick: I want be able to express code blocks like this...
<custom:CodeFormatter runat="server">
<asp:Label runat="server" ID="something" Text="my text" />
<asp:Image runat="server" ID="something" ImageUrl="header.jpg" />
</custom:CodeFormatter>
...but not have the inner tags actually execute. I want the Label & Image to be seen by CodeFormatter as raw text, not parsed as real server tags.
So my question is, how to prevent the Label from actually rendering as such? And how to access everything within the custom:CodeFormatter as raw, literal text?
My workaround right now is to encode the <>'s as < and >. I'd love not to do that.
Thanks!
How about extending your control to use ITemplate. Here is a article that explains how to extend a control.
http://www.devx.com/codemag/Article/32182