Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on 

source to code behind

I'd like to put these lines in a code behind set in C# instead  of in the html page so I can store it in a variable.

   You are here:
                  <a href="#" onclick="addlocation('Homepage.aspx')">Home</a>
                            <a href="#" onclick="addlocation('Visit.aspx')">/Visit</a>

Open in new window

C#

Avatar of undefined
Last Comment
zephyr_hex (Megan)
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

The question is a little strange.  You don't want HTML in your code behind.  I suspect addLocation() may do something that you're wanting to capture in code behind.  What does addLocation() do?
Avatar of zachvaldez
zachvaldez
Flag of United States of America image

ASKER

addlocation is just a javascript function that routes the page and it is ok

I just like to store the HTML text in a variable on a  hyperlink  control that functions the same thing but only in code behind
enclosed by " ".
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

The client-side code you specified calls a Javascript function called addlocation() which passes it a string value for the page you want. If you want to call the Javascript function from the server-side (the code behind set in C#), you need to use an ASP.NET control. The LinkButton comes to mind as a suitable control to use. There is a lot of online documentation regarding the LinkButton control. The gist however is as follows:
The client-side (HTML) for the LinkButton would be
<asp:LinkButton id="LinkButton1" Text="Home" OnClick="LinkButton1_Click" runat="server"/>

Open in new window

The OnClick function will generate the OnClick event handler for the server-side code. This will look similar to the following
public void LinkButton1_Click(Object sender, EventArgs e) 
{
   // Add whatever C# code you need to here
}

Open in new window

As a side note, if you need to call Javascript from the server-side, you would need to call the ScriptManager.RegisterStartupScript method. Also have a look at the ScriptManager.RegisterClientScriptBlock method.
Avatar of zachvaldez
zachvaldez
Flag of United States of America image

ASKER

I place this control in the masterpage. How will I access the control?
Avatar of zachvaldez
zachvaldez
Flag of United States of America image

ASKER

That's it ! Glad it worked
Using a string in c# for a HTML element is not the best way to do this.  You should be using  a ASP .NET control, or, creating a control from code behind with something like:

HyperLink link = new HyperLink();

Open in new window

C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo