How to pass C# variable to HTML code ?!

Posted on 2009-05-09
Last Modified: 2012-05-06
Hi there,

I wanna pass one public variable named v_public from C# code to HTML.
I used following code but it didn't work properly!
Is there any idea?

Thanks a lot.

<asp:TemplateField >
                <ItemTemplate >
                <asp:Image ID="URL" runat="server" ImageUrl= "<%=v_public %>" />

Open in new window

Question by:Super222
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 24343303
Remove the ImageUrl= "<%=v_public %>" from your html code, in your code behind you can access this property like so:

//URL is the ID of your ASP image tag.

URL.ImageURL = "YourUrl"

Expert Comment

ID: 24343310

try this i think it will work
<asp:TemplateField >
                <ItemTemplate >
                <asp:Image ID="URL" runat="server" ImageUrl= " '<%=v_public %>' "/>
or try
<asp:TemplateField >
                <ItemTemplate >
                <asp:Image ID="URL" runat="server" ImageUrl= <%=v_public %>/>

i think one them will definitely work

Author Comment

ID: 24343354
Thanks a lot for your quick reply.
@garethh86:I remoed the imageurl tag!! but in my ascx.cs code file this component was not recognized !!!!
@shrikantss: for this code nothing happend when i changed my code:
<asp:TemplateField >
                <ItemTemplate >
                <asp:Image ID="URL" runat="server" ImageUrl= " '<%=v_public %>' "/>
and for the second one i got this error message:
Details: Server tags cannot contain constructs
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 24343728
Is this a Templatefield in some databound repeater/gridview? If yes try:

<asp:Image ID="URL" runat="server" ImageUrl='<%# Eval("v_public") %>' />

Open in new window


Author Comment

ID: 24346830
Hi tillgeffken,
Thanks a lot for your reply.
Yes, it's a Templatefield in a gridview.
I want to get url from database in C# code page into one public variable then use its value in my HTML tag as image url.
I used the code that u mentioned but this error occurred.
Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'v_public'.
Line 11:                 <asp:TemplateField >
Line 12:                 <ItemTemplate>
Line 13:                 <asp:Image ID="URL12" runat="server" ImageUrl='<%# Eval("v_public") %>'/>
Line 14:                 </ItemTemplate>
Line 15:                 </asp:TemplateField>

Please help me to solve the problem ;)
Thanks a lot

  <asp:GridView   BorderWidth="0" ID="Show_News1" Width="100%"  runat="server" OnPageIndexChanging="gridView_PageIndexChanging" AutoGenerateColumns="False"   AllowPaging="True" AllowSorting="True" >
                <asp:TemplateField >
                <asp:Image ID="URL" runat="server" ImageUrl='<%# Eval("v_public") %>'/>
                <asp:BoundField HeaderText="" DataField="Title" ShowHeader="false" />
                <asp:BoundField DataField="News_Date" HeaderText="" ShowHeader="false" />
                <asp:HyperLinkField  HeaderText="" DataNavigateUrlFields="News_ID" DataTextField="News_ID" Target="_blank" DataNavigateUrlFormatString= "/Reports/News_Body.aspx?i={0}"/>
            <RowStyle Height="22px" BackColor="#fefbe2" ForeColor="#004274"/>
            <PagerStyle BackColor="#D7EBFF" />
            <AlternatingRowStyle BackColor="#e2f3fe" ForeColor="#004274"/>

Open in new window


Accepted Solution

tillgeffken earned 125 total points
ID: 24347576
That error means that its looking for a field named "v_public" but is unable to find it.

Whats the name of the column name holding your URL value in the database? Are you sure you're fetching it in your sqldatasource query? Remember it's case sensitive: (v_public != V_Public).

Please post the query you're using to pull the data and maybe the output of

EXEC sp_columns @table_name = 'YourTableName'

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question