how to call split method on designtime binding value - asp.net

Hi all
I have a grid with some templatecolumn , now i want to bind to on object property.
one of these property is a string type that i want split it in design time.
my code is here, but i have error. i know my code is wrong. please help me if any one know the right option to do this.
<telerik:GridTemplateColumn DataField="FilePath" UniqueName="FilePath" HeaderText="نام فایل" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"FilePath.Split('-').Last();")%>
</ItemTemplate>
</telerik:GridTemplateColumn>

You need to cast our Eval's expression to a type of string and then call Split method:
<ItemTemplate>
<%# ((string)Eval(FilePath)).Split('-').Last() %>
</ItemTemplate>

Related

Formatting int to currency-formatted string in aspx page

Suppose I have this label in my aspx page:
<asp:Label ID="savings" runat="server" Text='<%# Eval("savings")%>' />
Is there a way to format the text of a label as a currency-formatted string? I'm looking for something like this:
<asp:Label ID="savings" runat="server" Text='<%# Eval("savings").ToString("C")%>' />
When I run this I get the:
No overload for method 'ToString' takes 1 arguments
compilation error.
I know I can easily do this in my code-behind but I want to know if it's possible from the .aspx document.
Try this code:
<asp:Label ID="savings" runat="server" Text='<%# string.Format("{0:C}", Eval("savings"))%>' />
Try this code if you are dealing with string
<asp:Label ID="savings" runat="server" Text='<%# String.Format("{0:c}", Convert.ToDecimal(Eval("savings")))%>' />
The ToString(string) method only operates on numerical types, but Eval(string) returns object.
Cast the result to the correct type before calling the extension method on it, like this:
<%# ((decimal)Eval("savings")).ToString("C") %>
Late.. but useful
Anyone looking for Indian Currency Format or any other Currency use the CultureInfo class.
example:
(YourData).ToString("C2",CultureInfo.CreateSpecificCulture("in-IN"));
i would recommend that you convert the data you are trying to format to decimal in order to skip any overheads, also check this page for more culture combinations and more about formatting.

Get the this object using eval in a gridview in asp

I can't find any answer anywhere. I want to refer to the row object itself in an databinding expression in a gridview, like this:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server"
Text = '<%# GetPendingReason(Eval("this")) %>' />
</ItemTemplate>
</asp:TemplateField>
But it doesn't work because "this" doesn't refer to any attribute. Referencing individual attributes works fine, but how do you refer to the current row?
Simply use <%# Container.DataItem %>. Do not use Databinder.
If you want to refer to the current row you do that at codebehind using
GridViewRow row = GridView1.Rows[index];
at any of the GridView event.

asp.net databinding string is passed to function but runtime occurs

I'm using a code-behind function (called TestFx) in my binding expression. I'm passing a string and the function accepts a string but I still get a runtime error saying invalid args.
But if I change the method to accept an object and inspect the value, "it's a string!"
Can someone please explain?
-rod
ProductDescription:
<asp:Label ID="ProductDescriptionLabel" runat="server"
Text='<%# TestFx(Eval("ProductDescription")) %>' />
<br />
Another option is to handle repeater control ItemDataBound event. It's more suitable if ItemTemplate elements require complex decoration
The return type of Eval is object. As you've noticed, you can either change the signature of your method to accept an object, or you can typecast the result of Eval("ProductDescription") to a string:
<asp:Label ID="ProductDescriptionLabel" runat="server"
Text='<%# TestFx(Eval("ProductDescription").ToString()) %>' />

casting problem of star rating?

I have an application with ajax star rating but when i am assigning value to CurrentRating from datatable then it showing error of "Specified cast is not valid".
I am using this code.
<asp:TemplateField HeaderText="Rating" SortExpression="CustomerRating">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CustomerRating")%>'></asp:Label></a>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:Rating ID="Rating1" runat="server" CurrentRating='<%# Bind("CustomerRating") %>'
StarCssClass="ratingStar"
WaitingStarCssClass="savedRatingStar"
FilledStarCssClass="filledRatingStar"
EmptyStarCssClass="emptyRatingStar"
>
</cc1:Rating>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
Then its showing error CurrentRating='<%# Bind("CustomerRating") %>'.
I am taking refrence from these sites.
asp.net forum
Code Project
Same thing working on Code project.
The problem is most likely that the CustomerRating property of your data item is not of the correct data type. Rating expects an int. The Databinder does use reflection and attempts to automatically handle type conversions, but it has limits.
Unfortunatly there isn't enough information in your qustion to know what the actual runtime type of CustomerRating is, so I can't say why it can't be cast. My advise would be to explicitly cast or convert the property like so:
CurrentRating='<%# (string)Bind("CustomerRating") %>'
CurrentRating='<%# Bind("CustomerRating").ToString() %>'
CurrentRating='<%# (int)Bind("CustomerRating") %>'
If you can't convert it simply, or just need to get a debugger on it so you can figure out what the type is you can call out to a custom method in your code-behind instead (and you can attach a debugger there to so you can see the runtime type of the item:
CurrentRating='<%# MyCustomMethod(Eval("CustomerRating")) %>'
in code behind:
public string MyCustomMethod(object customerRating)
{
string rValue = ... //do whatever you need to do to customerRating to get a string out of it
// good place to set a breakpoint you you can examine what type customerRating actually is so you can figure out how best to convert it to something databinding can use
return rValue;
}

Can I negate the value in an attribute that uses an "Eval"?

I'd like to set a button's enabled state to be the negation of a value.
So given this code:
<asp:CheckBox ID="DefaultChecked"
Checked='<%# Bind("IsDefaultMessage") %>'
Enabled="false"
runat="server" />
<asp:LinkButton ID="MakeDefaultButton"
runat="server"
CommandName="MakeDefault'
CommandArgument='<%# Bind("ResidentialInfoID") %>'
Text="Make Default" />
How can I make the LinkButton Enabled attribute false if IsDefaultMessage == true?
Use Eval instead of Bind. Bind is for two-way binding, i.e. for cases where you need to be able to save the data back to your data source.
When you use Bind, the compiled page will actually have generated code that uses Eval to set the value, plus some code to read out the value for saving. Because Bind is replaced with generated code, you can't use any extra logic with Bind.
<asp:CheckBox ID="DefaultChecked" Checked='<%# !(bool)Eval("IsDefaultMessage") %>' Enabled="false" runat="server" />
<asp:LinkButton ID="MakeDefaultButton" runat="server" CommandName="MakeDefault' CommandArgument='<%#Bind("ResidentialInfoID") %>' Text="Make Default"/>
If you can use Eval, it is just a method of the Control class. It's only special in that it needs to be in the context of a data bound block <%# ... %>. Other than that, you can basically treat the block like a regular <%= %> expression block:
<%# !(bool)Eval("IsDefaultMessage") %>
If you want to still Bind it (Eval isn't round-trip), than you'll need to negate it back and forth during databinding. You may not need to do this though, if you can just re-word the control. For example, if a check box, instead of labelling it "Is Not Default Message" to the user and negating it back and forth, than lable it "Is Default Message". Contrived example, but you get the idea.
In case someone is looking for an option with VB.Net
<asp:CheckBox ID="DefaultChecked" Checked='<%# NOT (Eval("IsDefaultMessage")) %>' Enabled="false" runat="server" />
I've never used Bind but my understanding is that it is similar to Databinder.Eval. Either way, both methods return objects so you need to cast it to a boolean before evaluating it.
<%# !Convert.ToBoolean(Bind("IsDefaultMessage") %>
Edit: Looks like this can't be done and using a SqlDataSource on the page would solve the problem. http://forums.asp.net/t/1009497.aspx.
As I recall (It's been a while), there's no particular magic in <%#Bind(. It's just #Bind( inside <%....%>. Which means you'd want:
<% ! #Bind("IsDefaultMessage") %>'
The code
Checked='<%# Eval("IsDefaultMessage").ToString().Length() > 4 %>'
will return true if IsDefaultMessage is false
Since "False".Length = 5 and "True".Length = 4

Resources