multiple statments in inline c# on and asp.net page - c#

I have the following line in my aspx file:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>' Height="114" Width="152"/>
Is it possible to add another line to the inline c# something like this?
<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem); SetImageSize(this) %>' Height="114" Width="152"/>

I am afraid that this is not possible. But you could write another method on this helper class which will invoke the two operations at once.
<asp:Image
ID="Image1"
runat="server"
ImageUrl='<%# MediaHelper.GetMediaUrlAndSetImageSize(Container.DataItem, this) %>'
Height="114"
Width="152"
/>
Also mixing C# code with ASPX might lead to spaghetti. I would tend to avoid it as much as possible.

You could use multiple method calls to accomplish what you are trying to do:
<asp:Image
ID="Image1"
runat="server"
ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>'
Height="<%# MediaHelper.GetMediaHeight(Container.DataItem) %>"
Width="<%# MediaHelper.GetMediaWidth(Container.DataItem) %>"
/>
Or just bind an object to the control that has all of those values exposed as properties.

Related

Server tag is not well formed error on datagrid row ImageButton

I have come to an issue like the server tag is not well. this code is ASP.NET C# code.
<asp:TemplateColumn HeaderText="Options">
<ItemTemplate>
<asp:ImageButton ID="imgbuttEditVendor" runat="server" CommandName="Edit" CommandArgument="<%# Eval("ctpv_id") %>" ImageUrl="~/images/buttons/edit.gif" />
</ItemTemplate>
</asp:TemplateColumn>
Replace
CommandArgument="<%# Eval("ctpv_id") %>"
with
CommandArgument='<%# Eval("ctpv_id") %>'
Note: Embed double quote in the double quote is not allowed.

ASP Grid inside of Bootstrap popover using Eval Tags

I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"

How to Insert image on web page using image name pulled from access database?

I'm developing a website. I have an Access database that I pull image names from that i want to use as parameters to post images from my folder using C# code in VS 2010. I want to know how to access the image name as a string and insert it into my asp image element. I'm guessing I need to store the results of my "Select" query in a variable in order to pass it through the "Eval" function, but I'm just guessing and nor have I figured out how to do it.
The following section of code is where I established a connection with the database, used the Datalist control to list the information on my webpage that this code is for. And so what I'm trying to ultimately do is use the file name that's in the database as a parameter to grab the correct image from my files and post it to my web page. See below in code where I say FILENAME GOES HERE????? to see where I need to put my filename for my picture.
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT * FROM [Table2]"></asp:AccessDataSource>
<asp:DataList ID="DataList1" DataSourceID="AccessDataSource2"
ItemStyle-VerticalAlign="Middle" runat="server" DataKeyField="ID">
<ItemStyle VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Image:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PlaceImages/" Eval(*FILENAME GOES HERE?????*) %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
try this
<asp:Image ID="Image2" runat="server" ImageUrl='<%# "PlaceImages/" + Eval("Your_ImageName_Column")%>' />

Display value of Resource without Label or Literal control

How do I display the value of a resource without a ASP.NET control, i.e. I want to avoid this:
<asp:Label text="<%$ Resources: Messages, ThankYouLabel %>" id="label1" runat="server" />
Instead I would prefer to do just this in my .aspx pages:
<%$ Resources: Messages, ThankYouLabel %>
... but I can’t, a parser error is thrown:
Literal expressions like '<%$ Resources: Messages, ThankYouLabel %>' are not allowed.
Use <asp:Literal runat="server" Text="<%$ Resources: Messages, ThankYouLabel %>" /> instead.
Use HttpContext.GetGlobalResourceObject instead:
<asp:Label text='<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>'
id="label1"
runat="server" />
It's not possible. you have to use atleast Literal, Another option is to use GetGlobalResurceObject, so that you can use directly in a page.
<%= GetGlobalResourceObject("Messages", "ThankYouLabel")%>
In code behind You can Use
`GetLocalResourceObject("YourKeyInLocalResource")`
and also
`GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey")`
and then use a simple aspnet variable in your Asp.net Markup like <%= Resourcevalue %>
The you can assign your resource value to your Aspnet Variable like
Resourcevalue = GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey").ToString();
Another method is :-
<asp:Label text='<%= Resources.Messages.ThankYouLabel %>'
id="label1"
runat="server" />

server tags in markup

Morning all I have frequently used the old
<asp:Label ID="lblWas" runat="server" Text='<%# XPath("FACEVALUE") %>'></asp:Label>
This type of thing. when I first came across it I loved it, i'm using it again today but not quite so simply.
I have a number of extra things I would like to achieve.
Apply formatting to the value. Like Text='<%# string.Format(XPath("FACEVALUE"), "{0:c}") %>'>
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/' + XPath("FACEVALUE")>Book</asp:LinkButton>
For option number 2 the URL is not as I would expect, and for number 1 I cannot get the syntax correct if it's even possible.
I have not been able to find something suitable in google. Hopefully what I am trying to achieve is obvious from the example :)
You can use the TemplateControl.XPath(string xPathExpression, string format) override:
<asp:Label Text='<%# XPath("FACEVALUE", "{0:c}") %>' />
<asp:LinkButton Text="..." PostBackUrl='<%# XPath("FACEVALUE", "/THEATRE/{0}") %>' />
As you can see, you do not need to use string.Format because you can pass the format directly into the XPath method!
I believe for #1, you have messed up the syntax, you want to use
Text='<%# string.Format("{0:c}", XPath("FACEVALUE")) %>'
or Text='<%# XPath("FACEVALUE", "{0:c}") %>'
For #2, you need to use data binding expressions
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='<%# "/THEATRE/" + XPath("FACEVALUE")%>'>Book</asp:LinkButton>
For the first thing it must be Text='<%# string.Format( "{0:c}",XPath("FACEVALUE")) %>'>
and for the second one it should be
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/ + <%# XPath("FACEVALUE") %>'>Book</asp:LinkButton>

Categories

Resources