Format label text inside UpdatePanel via front end - c#

I have this label that get's its text from code behind. Due to the complexity of the code behind but mostly - not suppose to change any code behind - need to modify the label text on the front-end so it will look like currency.
Here is a label so far that sits inside the update panel:
<asp:Label ID="txtRFee" name="txtRFee" runat="server"></asp:Label>
This is what I added to the label:
<asp:Label ID="txtRFee" name="txtRFee" runat="server" Text="<%# Eval("txtRFee", "{0:c}") %>"></asp:Label>
It always comes out blank. Since it is sitting inside the asp:UpdatePanel inside the Eval when giving it ID need to specify that it sits inside the UpdatePanel but I tried pretty much everything. it will not compile. Tried txtRFee.ClientID etc
Text on the label comes out but Eval is being ignored. Any help is appreciated!

It looks like quotes are your problem. Change the outer quotes aroung the Text attribute to single quotes, like this:
<asp:Label ID="txtRFee" name="txtRFee" runat="server" Text='<%# Eval("txtRFee", "{0:c}") %>'></asp:Label>
There's a comment on this question that address the need for single quotes around an Eval() that uses double quotes. Good luck.

Related

I'm using a repeater to dynamically add labels to an asp.net page, but the text is stacking vertically for some reason. Anyone know why this happens?

Output
Code
<asp:Repeater ID="rptPBM" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="lblPBM" EnableViewState="false" Text='<%# Container.DataItem.ToString() %>'/>
<br />
</ItemTemplate>
</asp:Repeater>
I'm trying to add labels to a page dynamically using the above repeater, but when they are added it stacks vertically. Anyone ever encounter this before?
You are adding a line break after each label (br tag). Was that intended?
Remember that for each item in your data set it will write out that whole statement between the tags.
Maybe try writing out the html manually until you get it how you like and then put it into the item template.
Found the problem, the DataSource and DataBind() were in a loop so it was adding one letter at a time. Once I moved them out, it works fine.

unable to set value from server-side to a Textbox in client side

My code is like this
<asp:TextBox type="text" name="txtEndDate" Text="<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>" runat="server"></asp:TextBox>
Everything looks okay to me, But i don't know why its throwing an error
The server tag is not well formed.
Use single quotes like:
<asp:TextBox type="text" name="txtEndDate" Text='<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>' runat="server"></asp:TextBox>
The issue is that because of the ASP.net brackets which contain double quotes inside them
<asp:TextBox Text="<%# someMethod("someValue") %>" />
inside the Text field, you need to use single quotes instead of double quotes on that property, like this:
<asp:TextBox type="text" name="txtEndDate"
Text='<%# library.GetDictionaryItem("ProfilePages_CVR nummer")%>'
runat="server">
</asp:TextBox>
And it will work.
Note also that you are using the DataBinding notation ( <%# ) which will only work if your TextBox is inside a DataBound control, or you call DataBind on the control or page containing this TextBox.

Surrounding Eval with html tags

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")) %>

Show and hide labels with assosicatedControls and their controls

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?
<asp:label AssociatedControlID="thisLabel" runat="server">This:
<asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>
I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.
You haven't supplied a server-side Id:
<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />
What you've done is nest a asp:Label control within another asp:Label control....
Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.
Just my nickel's worth, your mileage may vary, as always.
It should work if you get you r markup correct, like this:
<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):"
AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>
Then in the code behind you could have:
lblYear.visible = False
txtYear.Visible = False
Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

Label text in datarepeater

I am trying to use a label in my datarepeater, when I am able to bind data and write to me html page.
<asp:Label ID="lblID" runat="server"><%# DataBinder.Eval(Container.DataItem, "ID")%></asp:Label>
which works fine.
When I try to get text value I get "".
Label lblcurrentID = ri.FindControl("lblID") as Label;
result:
lblcurrentID.text = ""
this same code works fine for the dropdownlist that I have in the datarepeater. I am wondering if this has anything to do with the label being converted to a span tag.
<span id="template6_middlecontent1mAzoaNominationApproval0_dataReaper_ctl01_lblID">2009040100000888213</span>
I've ran into this issue before, I don't recall actually finding a solution to the problem, as a work around, I used an <asp:HiddenField> to hold onto the information for me:
<asp:HiddenField runat="server" id="hiddenId" value='<%# Eval("Id") %>' />
Note the ' instead of " wrapping the Eval statement btw, .NET is fussy when you're assigning values to server controls.
you need bind lblD.Text with the value not like
<%# DataBinder.Eval(Container.DataItem, "ID")%>

Categories

Resources