Link to home
Create AccountLog in
Avatar of Super222
Super222

asked on

How to pass C# variable to HTML code ?!

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 %>" />
                </ItemTemplate>
                </asp:TemplateField>

Open in new window

Avatar of garethh86
garethh86
Flag of United Kingdom of Great Britain and Northern Ireland image

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"

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


i think one them will definitely work
Avatar of Super222
Super222

ASKER

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 %>' "/>
                </ItemTemplate>
                </asp:TemplateField>
and for the second one i got this error message:
Details: Server tags cannot contain constructs
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

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" >
               
                <Columns>
                <asp:TemplateField >
                <ItemTemplate>
                <asp:Image ID="URL" runat="server" ImageUrl='<%# Eval("v_public") %>'/>
                </ItemTemplate>
                </asp:TemplateField>
                <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}"/>
                </Columns>
            <RowStyle Height="22px" BackColor="#fefbe2" ForeColor="#004274"/>
            <PagerStyle BackColor="#D7EBFF" />
            <AlternatingRowStyle BackColor="#e2f3fe" ForeColor="#004274"/>
            </asp:GridView>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tillgeffken
tillgeffken

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer