What is the syntax to concatenate text into a binding expression for an asp.net webpage (aspx).
For example if I had a hyperlink that was being bound like this:
<asp:HyperLink id="lnkID" NavigateUrl='<%# Bind("Link") %>' Target="_blank"
Text="View" runat="server"/>
How do you change, say, the Text to concatenate a bound value with a string? Variations like this aren't quite right.
Text='<%# Bind("ID") + " View" %>'
neither does
Text='<%# String.Concat(Bind("ID"), " View") %>'
Use Eval instead.
Text='<%# Eval("ID", "{0} View") %>'
Eval is also better if the value is not going to be updated, where Bind allows two way data binding.
You can also place the "concatenation" in the text portion of a tag if using a template field:
<asp:TemplateField HeaderText="Name" SortExpression="sortName">
<ItemTemplate>
<asp:LinkButton ID="lbName" runat="server" OnClick="lbName_Click" CommandArgument='<%# Eval("ID") %>'>
<%--Enter any text / eval bindind you want between the tags--%>
<%# Eval("Name") %> (<%# Eval("ID") %>)
</asp:LinkButton>
</ItemTemplate>
This results in output like:
Name (ID)
inside of the template column.
I have used String.Format("{0}{1}"... before to good effect.
You could use the following:
CommandArgument='<%#String.Format("{0}|{1}", Eval("ArgZero"), Eval("ArgOn"))%>'
Related
I have a grid view template column that is a glyphicon in a hyperlink control, I am trying to pass an ID value that is in the SQL datasource to a page like so :
<ItemTemplate>
<asp:HyperLink ID="hlResident" CssClass="glyphicon glyphicon-refresh" NavigateUrl="Resident/<%#= Eval("ID") %>" runat="server"></asp:HyperLink>
</ItemTemplate>
I get the error "The Server Tag is not well formed".
Try this way
<asp:HyperLink ID="hlResident" CssClass="glyphicon glyphicon-refresh" NavigateUrl='<%# "Resident/" + Eval("ID") %>' runat="server"></asp:HyperLink>
Here the change is how you bind it
NavigateUrl='<%# "Resident/" + Eval("ID") %>'
<%#= Eval("ID") %>
Should be:
<%# Eval("ID") %>
There are some different types for asp tags. The accepted answer Here is very thorough on the subject.
NavigateUrl="Resident/<%#= Eval("ID") %>"
should be NavigateUrl='Resident/<%# Eval("ID") %>'
I am binding a Gridview with some data and I have taken labels in template field and I am binding the data to that label's text property,
<asp:TemplateField HeaderText="Proceedings" SortExpression="PROCEEDINGS" ItemStyle-Width="60px">
<ItemTemplate>
<asp:Label ID="lblProceedings" runat="server" CssClass="Label_Value" Text='<%# Bind("PROCEEDINGS") %>' ToolTip='<%# Bind("PROCEEDINGS") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="true" HorizontalAlign="Left" />
I want the length of the Bound text till 10 characters how to achieve this
Try this:
<asp:Label ID="lblProceedings" runat="server" CssClass="Label_Value" Text='<%# Eval("PROCEEDINGS") == null ? "empty" : Eval("PROCEEDINGS").ToString().Substring(0,10)%>'
or check this link Substring in a label
I hope that help.
10Label has no MaxLangth property, you can use here textbox and give MaxLength="10", and style it as a lable.
EDIT:
You can use substring like this:
<asp:label id="lDesc" runat ="server" text ='<%# (Eval("Description") .Length>=10) ? Eval("Description").SubString(0,10) :Eval("Description") %>'></asp:Label>
You can use like this
Text='<%# Eval("PROCEEDINGS").ToString().Substring(0,9) %>'
Or you can write a function in .cs file like this.
protected getString(string str)
{
return (str..Substring(0,9));
}
And use this in gridview label as
Text='<%# getString(Eval("PROCEEDINGS").ToString()) %>'
This is dataGrid . How to add text(string) after <%# Bind("Value") %>
for example 123.432 and i want after any record to have "$" dolar sign
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
<% String.Format("{0}$",Eval("Value")); %>
You can take benefit of Standard Numeric Format String
<asp:Label ID="LabelValue" runat="server"
text='<%# Bind("Value").ToString("C", CultureInfo.CurrentCulture) %>'/>
which is a standard format available with C#.
But you want to show it in the last then you can simply add it in the last.
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
$
</ItemTemplate>
I suggest you to use MaskedEdit extender in ajax toolkit if you already using ajax toolkit in your project. MaskedEdit extender can be use in edit template and it will handle the mask on client side you don't want to worry about value and $ sign when you read the value back.
without using ajax toolkit you can use one label for $ sign and textbox for value field on edit template. In normal template also use two labels for both value and $ sign. then it will be easy to read the values.
I have been trying to get these running looking at lots of different samples but with no luck.
To me it seems it's ok but what am I missing here?
<asp:Label ID="Label1" runat="server" Text="<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>" Width="100px" visible="true"></asp:Label>
Thanks
"With no luck" is not a descriptive error.
<%# is for databinding expression only. So have you called Page.DataBind() or at least Label1.DataBind() in codebehind?
You could also try
Text='<%= String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>'
Apart from that, why not doing such things in codebehind only, so you don't have issues like this?
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>' Width="100px" visible="true"></asp:Label>
you shouldn't use Text="something". you should use Text='something'
I am trying to concat a string inside the attribute. I get an error. I think that it is related to my Eval. Is there a proper way to concat strings, or is this just not possible. The problem I believe is where I set the NavigateUrl.
<asp:HyperLink ID="lb"
runat="server"
Text='<%#Eval("Key.Id") %>'
NavigateUrl='ViewItem.aspx?id=' + '<%# Eval("Key.Id") %>'/>
Short answer: NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'
Longer explanation:
The problem in your code is that you are use data binding expression only for part of your web control attribute. You need to move everything inside the data binding expression.
First of all, a data binding expression is this:
<%# EXPRESSION %>
Basically the rule for using a data binding expression for a web control attribute is that the expression must be the only thing in the attribute:
<asp:HyperLink ID="lb" runat="server"
Text='<%# EXPRESSION %>'
NavigateUrl='<%# EXPRESSION %>' />
So your first attribute, Text, is correct. But your second attribute, NavigateUrl is not correct. Because you put ViewItem.aspx?id= as the value for the attribute, leaving + '<%# Eval("Key.Id") %>' outside any attribute but inside the control tag.
Here is the correct syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'/>
Notice that we are using a format string as the second parameter for Eval(). This is equivalent to the following, more explicit, syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# String.Format("ViewItem.aspx?id={0}", Eval("Key.Id")) %>'/>
Here's what I do when I have something in a gridview like this:
<img src='<%# GetDisImageLink(Eval("Disabilities").ToString()) %>'
alt="Disabilities" />
[CS code-behind]
public string GetDisImageLink(string dis)
{
return "../../Content/Images/CardContactInfo/" +
(dis.Trim() == "Y" ? "DIS.png" : "Blank.png");
}
Try this instead:
<asp:HyperLink ID="lb" runat="server" Text='<%#Eval("Key.Id") %>' NavigateUrl='ViewItem.aspx?id=<%# Eval("Key.Id") %>'/>
You don't need to concatenate