I have looked for other solutions here on StackOverflow but i cant seem to figure it out.
I have this linkbutton in a datagrid:
<asp:LinkButton
ID="lnkname" runat="server"
Text='<%#Eval("Titel") %>'
PostBackUrl='<%#"Details.aspx?ID="+Eval("ID").toString()%>'
CausesValidation="false">
</asp:LinkButton>
and this is the paramater:
<asp:ControlParameter ControlID="txtTitel" DefaultValue="*" Name="Titel"
PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />
I want to take the ID value to the next page : details.aspx but i get the following error:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
I know it has something to do with ID being a INT but I cant figure out how to fix it.
NOTE: I do it in VB.NET
There is problem in casting it to string
+Eval("ID").toString()
use this
PostBackUrl='<%# "Details.aspx?ID="+Eval("ID").ToString() %>'
The .toString() should be ToString()
Edit 1
Or you can use
Try using a HyperLinkField
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.aspx
<asp:HyperLinkField
HeaderText="Client IP"
DataNavigateUrlFields="IP"
DataNavigateUrlFormatString="Details.aspx?id={0}"
DataTextField="ID"
DataTextFormatString="{0}"/>
If you need to pass multiple parameters then you can use & between them For example
PostBackUrl='<%# "Details.aspx?ID="+Eval("ID")+"&x=50" %>'
Important
Whenever you are concatenating(joining) a variable of any type like Eval("ID") value to a string like "Details.aspx?ID=", You do not need to write ToString() for casting becasue concatenation does this casting implicitly (automatically). I am sure about this behaviour for C# and VB.NET
So you could write simply
PostBackUrl='<%# "Details.aspx?ID="+Eval("ID") %>'
Related
I am building a ListView from an EntityDataSource. There is a Byte[] field called Sha1Hash that I need to convert to a String type for a Hyperlink. This is a code snippet from ItemTemplate:
<asp:HyperLink ID="hl_Document" runat="server"
NavigateUrl='<%# string.Format("~/GetDocument.ashx?docId={0}", Eval("SHA1HASH") ) %>'
Text='<%# Eval("DOCUMENTNAME") %>' />
This is producing a URL like "~/GetDocument.ashx?docId=System.Byte[]", but it needs to be a hex string representing the value of the byte array. What would be the best approach to accomplish this? Ideally I would accomplish this entirely in the .aspx page rather than the code behind.
Update: I resolved this issue by using
NavigateUrl='<%# string.Format("~/GetDocument.ashx?docId={0}", BitConverter.ToString((byte[])Eval("SHA1HASH")).Replace("-", string.Empty) ) %>'
My problem was that I needed to cast Eval() to byte[]. It was being cast to string.
Depending on what format you want, you're probably looking for Convert.ToBase64String() or BitConverter.ToHexString().
I resolved this issue by using
NavigateUrl='<%# string.Format("~/GetDocument.ashx?docId={0}", BitConverter.ToString((byte[])Eval("SHA1HASH")).Replace("-", string.Empty) ) %>'
My problem was that I needed to cast Eval() to byte[].
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.
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()) %>' />
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;
}
I'm trying to create a LinqDataSource to bind to a DropDownList in an ASP.NET form. I only want to show the elements according to a date (which is one of the fields in the database).
Basically, the elements I want to show are the ones that will happen in the futures (i.e. after DateTime.Now).
I was trying the following markup :
<asp:DropDownList runat="server" ID="DropDownList1"
AppendDataBoundItems="True" DataSourceID="LinqDataSource1"
DataTextField="TextField" DataValueField="ValueField">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataContext1" TableName="Table"
Where="DateField >= #DateField">
<WhereParameters>
<asp:Parameter DefaultValue="DateTime.Now" Name="DateField"
Type="DateTime" />
</WhereParameters>
</asp:LinqDataSource>
I'm getting a format exception saying that "The string was not recognized as a valid DateTime" when I try to run it. However, the dates in my database seem to be fine, because a DateTime.Parse works perfectly on them. The DateField is of type datetime in SQL.
What am I missing here?
Thanks!
The DefaultValue was what was wrong with the code as was suggested by the others.
However, setting the DefaultValue to
"<%# DateTime.Now %>"
like Andomar suggested (which would make the markup look something like this :
<WhereParameters>
<asp:Parameter DefaultValue="<%# DateTime.Now %>" Name="DateField" Type="DateTime" />
</WhereParameters>
will not work either because DataBinding expressions are only supported on objects that have a DataBinding Event, and neither Parameter or ControlParameter have one.
For a String, it's fairly easy to create a TextBox or Label and put the <%# %> expression in the value of that new field (more details here), but it was a bit more complicated with a DateTime value, as comparing an SQL DateTime with a .NET DateTime caused an exception.
It can be done quite easily in the Page_Load event by using
DataContext DataContext1 = new DataContext();
var c = from a in DataContext1.Field
where a.DateField >= DateTime.Now
select a;
DropDownList.DataSource = c;
DropDownList.DataBind();
I suspect it is failing on the DefaultValue.
Try:
DefaultValue="<%# DateTime.Now %>"