How to cast float to integer in ASP.NET? - asp.net

I am trying to assign a specific width to my image in asp.net (using vb.net).
So I used the next code:
<asp:Image ID="PercentageImage" runat="server" Height="7px"
Width='<%# Eval("votsGraph") %>'
ImageUrl="~/images/bar.JPG" />
So how do i cast this one?
(The type of votsGraph is float.)

Use CInt():
<asp:Image ID="PercentageImage" runat="server" Height="7px" Width='<%# string.format("{0}px", cint(Eval("votsGraph"))) %>' ImageUrl="~/images/bar.JPG" />
I've also added String.Format() to your code as you were missing px:
Width='<%# string.format("{0}px", cint(Eval("votsGraph"))) %>'

Related

Image not getting displayed in datalist

I am binding images to datalist. Taking the image name from database and giving the path.
My code is:
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" ImageUrl='<%# Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
On .cs page:
ds = gc.GetDataToListBinder("select DISTINCT PageOrderID,PageName from ScreenMaster order by PageOrderID")
dlImages.DataSource = ds.Tables(0)
dlImages.DataBind()
I am facing 2 problems :
When imagename has space in between it adds %20 in between
Eg. if imagename is "API Message", it takes it as: "API%20Message"
I tried On this Problem:
Added ImageUrl='<%#Server.HtmlDecode(Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg"))'
But I got error:
XML literals and XML properties are not supported in embedded code within ASP.NET
If there is not space Eg.image name is "Charges" , Then also its not showing it in datalist.
When i ran project, and right clicked on it and view source, then its showing me correct path as:
src="D:\Sagar\Kinston\WebSite\ScreenMasterImages\Charges.jpg"
but not showing image.
Please help me with above code.
Where i have made mistake?
What else i should add in it?
Keep it easy and simple.
When trying things like that with URL, to know exactly what to write down, try typing it in your address bar so you'll be able to find the exact syntax needed in order to make it work correctly.
I've done this tons of time and work for me... So you could try something like :
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px"
ImageUrl='<%# String.Format("{0}{1}.jpg", "~\ScreenMasterImages\", Server.HtmlDecode(Eval("PageName"))) %>'
runat="server" />
</ItemTemplate>
</asp:DataList>

Which property or method should i use to bind the ToolTip property to an ImageField Class?

In an image control that is bound within a GridView column i can set the Alt text property by using the DataAlternateTextField property.
<asp:ImageField DataImageUrlField="Flag"
DataImageUrlFormatString="~/images/f/{0}.png"
DataAlternateTextField="Description" >
</asp:ImageField>
which results in somathing like...
<img src="1.png" alt="Alt text"/>
Which property or method should i use to bind the ToolTip property?
<img src="1.png" alt="Alt text" ToolTip="Title text" />
I would like the users of the website, on mouse hovering of the image, to be able to read the title property.
To all newbies out there just like me, here what i did
<asp:TemplateField HeaderText="Test" SortExpression="">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("Flag", "~/images/f/{0}.png") %>'
AlternateText='<%# Bind("Description") %>'
ToolTip='<%# Bind("Description") %>' />
</ItemTemplate>
</asp:TemplateField>
Thanks Veronica
You can use binding expression to set the title of the ImageField. http://msdn.microsoft.com/en-us/library/ms178366.aspx
I am playing with this issue now according to Microsoft, the alternate text property of an ImageField should translate into a tooltip as well. At least that's what it says on the msdn page i just linked.

image url Concatenate string

i want to achievet the following Url
ImageUrl='~/products_pictures/(imageId)_middle.jpg
im using gridview and datalist
im trying the follwoing combination but it not working
<asp:Image ID="Image1" ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg' runat="server" /></td>
<asp:Image ID="Image1" ImageUrl=<%"~/products_pictures/"%><%#Eval("Id")%><%"_middle.jpg"%> runat="server" />
I would use String.Format for this. It makes concatenation much easier:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/products_pictures/{0}_middle.jpg", Eval("ID"))%>'
this should work:
</td><asp:Image ID="Image1" ImageUrl="~/products_pictures/<%#Eval("Id")%>_middle.jpg" runat="server" /></td>
if not debug from this:
</td><%#Eval("Id")%></td>
ells you can try
<%# ((objectName)Container.DataItem).Id%>
((DataRowView)Container.DataItem)["Id"]
This might work
<asp:Image ID="Image1" runat="server" ImageUrl="~/products_pictures/<%#Eval("id") %>_middle.jpg"/>
If not you can put a asp:Literal, and a HiddenField to store id and in GridView.RowDataBound Event you can add image as a text to the literal
The server tag is not well formed because of the quotes
put single quotes on the outside and try again
ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg'

ASP.NET How to pass container value as javascript argument

I am using an oBout Grid control with a template on a textbox.
I would like to pass an argument to a javascript, the current row index of a grid when a user clicks on it.
But the result of
onClick='setGridInEditMode(<%# Container.RecordIndex %>);' />
comes out as
onClick="setGridInEditMode(<%# Container.RecordIndex %>);"
Is there a way to pass container value to javascript?
Here is the markup in question.
<cc1:Grid ID="_TrustGrid" runat="server"
FolderStyle="Styles/style_7"
AllowAddingRecords="False"
AllowSorting="false"
AllowPageSizeSelection="False"
AllowPaging="False"
AllowMultiRecordEditing="true"
AutoGenerateColumns="False"
OnUpdatecommand="_TrustGrid_UpdateCommand"
OnRebind="_TrustGrid_Rebind">
<Columns>
<cc1:Column AllowEdit="true" AllowDelete="false" HeaderText="Edit" Width="130" runat="server" />
<cc1:Column DataField="TrustDocID" HeaderText="TrustDocID" Width="125" ReadOnly="false" AllowDelete="false" TemplateId="trustDocIDGridTemplate" />
</Columns>
<Templates>
<cc1:GridTemplate ID="trustDocIDGridTemplate" ControlID="tb1" runat="server">
<Template>
<asp:TextBox ID="trustDocIDTextBox" runat="server"
Visible="true"
Text='<%# Container.Value %>'
onClick= 'setGridInEditMode(<%# Container.RecordIndex %>);' />
</Template>
</cc1:GridTemplate>
</Templates>
</cc1:Grid>
I'd second Darin's call for using unobtrusive JavaScript. However, that doesn't answer your question on why ASP.NET is doing this.
The reason you get
onClick="setGridInEditMode(<%# Container.RecordIndex %>);"
is because databinding to server control properties requires you to bind directly to the property without intervening text. That means, only Property="<%# ... %>" is allowed.
So in your case, you'll need to say what you want in a roundabout fashion (although I personally think this is a little clearer and more maintainable):
onClick='<%# String.Format("setGridInEditMode({0});", Container.RecordIndex) %>'
(Watch your single and double quotes though!)
This limitation applies only to server controls and their properties. It does not apply to a server control's nested literal content (such as bodies of templates or panels) nor to plain HTML used elsewhere, which is probably why you've never noticed this before.
Instead of polluting your HTML with javascript functions how about an unobtrusive solution using jQuery:
$(function() {
$('#_TrustGrid input[id*=trustDocIDTextBox]').each(function(index) {
$(this).click(function() {
setGridInEditMode(index);
});
});
});
If you prefer instead the more ASP.NETish solution you could always do this:
<asp:TextBox
ID="trustDocIDTextBox"
runat="server"
Visible="true"
Text='<%# Container.Value %>'
onclick='<%# "setGridInEditMode(" + Container.RecordIndex + ")" %>' />

asp:DataList control with asp:LinkButton inside - something's weird

I'm working through examples in a book trying to learn ASP.NET, and I've stumbled on something strange in there. First of all, if I type it as it's written in the book, VS gives me errors. This is the code as it's written in the book:
<asp:DataList ID="employeesList" runat="server">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text=<%#"View more details about " + Eval("Name")%>
CommandName="MoreDetailsPlease"
CommandArgument=<%#Eval("EmployeeID")%> />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
So, I've plucked at it for a while, and came up with this solution which actually compiles:
<asp:DataList ID="employeesList" runat="server" onitemcommand="employeesList_ItemCommand">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text='View more details about <%# Eval("Name") %>'
CommandName="MoreDetailsPlease" CommandArgument='<%Eval("EmployeeID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
Notice that I've also added the OnItemCommand in the asp:DataList tag, so now I'm able to fire the event as expected.
However, results in the browser isn't what I expect; the Name and Username listed in strong text show just fine, but the Literal control that should show extra details (the EmployeeID field) and the Name field inside the LinkButton won't show their values in the page:
not showing as expected http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg
It should say "EmployeeID: 1" and "View more details about Zak Ruvalcaba"
I guess it's the Eval function that's not working when inside another control, can anyone help me out?
Change the LinkButton as :
<asp:LinkButton ID="detailsButton" runat="server"
Text='<%# Eval("Name", "View more details about {0}") %>'
CommandName="MoreDetailsPlease"
CommandArgument='<%# Eval("EmployeeID") %>' />
Sorry I confused the order of parameters. I updated my answer. Format must be the second parameter.
You can view another question I posted yesterday concerning something eerily similar here:
Need help with Eval inside DataList
I do believe Canavar actually gave the correct answer, however.

Resources