I would like to insert a string (key) into a '<%$ Resources:resFile, someKey %>' statement dynamically at runtime.
Right now I have this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, someKey %>'/>
which is useless to me, because the key "someKey" is static and will always converge to the same x translations.
I've tried doing things like this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, '<%#Eval("someProperty")%>' %>'/>
But this doesn't quite compile. I'm not sure if you can have nested <%%> statements, so thats why i'm asking here.
I've also tried a variety of things with GetGlobalResourceObject() but that doesn't do what i want either.
The thing is that i am binding a list to a repeater, and one of the items in the datasource will contain the key that will match a key in the resource files.
If anyone knows a way how to achieve this, be it with nested <%%> statements or be it any other way, i'd be happy to try it out.
You could do this in Code-Behind on the ItemDataBound Event.
Label lv = (Label)e.Item.FindControl("lbl");
ResourceManager resMngr = new ResourceManager(typeof(SupertextCommon.Default));
lv.Text = resMngr.GetObject(someProperty, culture);
This has worked for me in a GridView using a property of its datasource:
<%# GetGlobalResourceObject("Resource", Eval("SomeProperty").ToString()) %>
You can also use concatenation to build the key, e.g.:
<%# GetGlobalResourceObject("Resource", "SomePrefix" & Eval("SomeProperty").ToString()) %>
Related
Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?
I'm just learning ASP.Net, so I hope that you bear with me and my questions. In my program, I have a dataset that contains Url strings that point to various images. My question is, can I use that dataset as a datasource for a Repeater control so that the Repeater uses those Urls to display the images?
Thanks so much for any help and advice.
You most certainly can. You will want to do the binding in the code-behind, probably in the Load method like this:
repeaterControl.DataSource = yourDataSet.Tables[0];
repeaterControl.DataBind();
where 0 is the index of the DataTable you're trying to get to.
Then you'll want to build the markup something like this:
<asp:Repeater ID="repeaterControl" runat="server">
<ItemTemplate>
<asp:Image runat="server"
ImageUrl="<%# DataBinder.Eval(Container.DataItem, "TheFieldName") %>" />
</ItemTemplate>
</asp:Repeater>
where TheFieldName is the name of the field/column in the DataTable that contains the URL. Now, this code may need to be debugged a little bit because I didn't build an entire project around this, but this will get you 99% of the way there, if not all of the way.
Please please help!! I've been searching this for hours but maybe the solution is so obvious I'm completely overlooking it :(
I have a listview that binds to a sql table. The table contains a bunch of fields that hold only 'T' or NULL values, except for the last two fields which hold free-type text for user comments.
For the T/NULL values - The listview is configured so that an empty cell will not display, and where there is a 'T' value, it will instead show a custom text and create a line break for the next item. This syntax does exactly that:
<asp:Label ID="LymeLabel" runat="server" Text='<%# Eval("Lyme","Lyme Disease<br />") %>' />
However, I want the last two fields (the free-type text) do display in a paragraph style, with paragraph tags surrounding the Eval statement. The tags have to form part of the Eval so that they won't render whitespace if the cells are empty. My thinking was to do it this way but it doesn't work - the whitespace shows up either way:
<asp:Label ID="OtherCommentLabel" runat="server" Text='<%# "<p>"+Eval("OtherComment")+"</p>" %>' />
I'm guessing the " "+ ... +" " doesn't care what's in or not in the middle.. but how to fix it? There must be a very simple solution to this and I feel really stupid even asking this. Any advice please?
From the description, you don't need the asp:Label at all. Why not just wrap the Eval() in a P tag?
<p><%# Eval("Lyme","Lyme Disease") %></p>
Even if the label worked, you wouldn't want the resulting markup (paragraph inside a label makes no sense).
To handle the show/hide if the item is empty, you could do something like:
<ItemTemplate>
<p runat="server" visible='<%#!string.IsNullOrEmpty(Eval("OtherComment"))%>'>
<%# Eval("Lyme","Lyme Disease") %>
</p>
</ItemTemplate>
The white space will show up because p is not part of the Eval; <p>Eval()</p>.
Not sure if elegant, but you could wrap the Eval inside a method:
public string ParagraphIfData(string input)
{
if(!string.IsNullOrEmpty(input))
return "<p>" + input + "</p>";
return "";
}
Then:
<%# ParagraphIfData(Eval("Lyme","Lyme Disease")) %>
I am trying to Concat to fields in the SelectedValye property of the Dropdownlist. I want to use Bind and not Eval. This is what I am using but it's not working. Is this possible to do?
<asp:DropDownList ID="RsmList" runat="server" DataSource="<%# ddRSM.DataSource %>" SelectedValue='<%# Bind("RSMLast") + '', '' + Bind("RSMFirst") %>'>
</asp:DropDownList>
Thanks!
I dont believe you can without using Eval because Bind translates actually to two separate method calls. You need to either use Eval() or do it before you databind. But - happy to be proven wrong : )
I don't think you need your DataSource inside binding tags -- I don't set this stuff up in markup, but I believe your reference should just be DataSource="ddRSM.DataSource".
You should also be able to set your SelectedValue property like so:
SelectedValue='<%# Bind("RSMLast") %>, <%# Bind("RSMFirst") %>'
Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}