ASP.NET Eval Byte Array as String - asp.net

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[].

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.

how to call split method on designtime binding value

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>

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()) %>' />

Listview/DetailsView: Hide a null field

I imagine this is quite a common problem, but as yet I haven't found an elegant solution.
I have a number of instances where I have either a ListView or a DetailsView control that is bound to a SQL Server SProc. The problem I have is that there are numerous instances where, when a column is Null, I want to display something different in the UI. A typical example would be where I have a URL column that is rendered as a LinkButton (in the ListViews) or as a HyperLinkField (in the DetailsViews) - when a Null URL is returned, I'm rendering links with no src attribute. Ideally, I's want to display nothing in this field in such a scenario.
In each of these cases, when a null value is returned, how can I skip/alter the rendering of that item?
Thanks in advance.
Update: I haven't had chance to actually try these out, but all helpful suggestions. I think I like Ricks answer the best, but thanks again to the others...
Markup:
<asp:HyperLink id="whatever" runat="server"
NavigateURL='<%# Eval("url") %>' Visible='<%# IsVisible(Eval("url")) %>' />
Code behind:
protected bool IsVisible(object obj)
{
bool result = false;
string url = (string)obj;
if(!string.IsNullOrEmpty(url))
{
result = true;
}
return result;
}
Within a Template bind also to Visibility
<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != null %> />
Warning: not Tested, could also be
<asp:HyperLink ... NavigateURL=<%# Eval("url") %> Visible=<%# Eval("url") != DBNull.Value %> />
I suppose you could either create a method in your code behind that takes the value as a parameter and returns a link if it's not null. Or you could tap into the data bound event of the Listview, examine the value and hide the control if it's null.
Neither a very elegant solutions, but I guess that's up to you to decide. :)

How do I concatenate 2 resource strings together in an aspx page

I have a localised ASP.net application (.net 2.0). I wish to concatenate 2 strings retrieved from the resource file together into one element, something like this.
Text="<%$ Resources:Resource, lw_name %>" + <%$ Resources:Resource, lw_required %>"
I have tried using Eval without success. Is what I am trying to do the "correct" approach or can I store strings with placeholders in the resource file and interpolate them "on the fly".
I am trying to do this in the aspx file rather than in code-behind.
ASP.NET tag attribute values that use <%$ Something: Something Else %> are of a special syntax called ASP.NET Expressions. Using them as attribute values are pretty much all-or-nothing; there's no way to add any code into the ASPX file to manipulate what those expressions evaluate to. You'll have to do this in the code-behind.
I search for the solution so long
This code works for me:
ToolTip='<%# Resources.Global.Btn_Edit + "/" + Resources.Global.Btn_contact %>'
< asp:HyperLink ToolTip='<%# "Some Text :" + Eval("id").ToString() %>' ....../>
Do you mean something like this.... ToolTip='...' -> Convert your return values to STRING... ( xxxx.ToString() )
Like this it displays: Some Text: 1234 --> on Tooltip
so you should do something like this in your case:
Text="<%$ (Resources:Resource, lw_name).ToString() %>" + <%$ (Resources:Resource, lw_required).ToString() %>"
I don't know if it's going to work but try to conver to ToString().
I know you said you tried eval but what about something like this:
Text='<%# string.Format("{0}{1}",Eval("lw_name"),Eval("lw_required")) %>'
I was having the same issue, and I solved it by using this option instead:
Text="<%= HttpContext.GetGlobalResourceObject("Resource", "lw_name") %> <%= HttpContext.GetGlobalResourceObject("Resource", "lw_required") %>"
For local Resources, use the GetLocalResourceObject method instead of GetGlobalResourceObject
Try
"#(Resources.ResourceString + Resources.ResourceString)"
Use this method to append 2 strings in ASPX.
Text='<%# String.Format("{0} {1}",
Resources.file01.string1,Resources.file01.string2)%>'
This Might Be of Help
<asp:Label ID="Mylabel" runat="server">
<%= "before Message String- "+ Resources.MyResources.Message +" -After Message String " %>
</asp:Label>
Note the concatenation is not on the Text attribute but between the label element
Full post can be found here

Resources