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

zachvaldezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
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?
0
zachvaldezAuthor Commented:
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 " ".
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you can try:

String link1 = @"<a href=""#"" onclick=""addlocation('Homepage.aspx')"">Home</a>";
            String link2 = @"<a href=""#"" onclick=""addlocation('Visit.aspx')"">/Visit</a>";

Open in new window


or

String link1 = "<a href=\"#\" onclick=\"addlocation('Homepage.aspx')\">Home</a>";
            String link2 = "<a href=\"#\" onclick=\"addlocation('Visit.aspx')\">/Visit</a>";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Dirk StraussSenior Full Stack DeveloperCommented:
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.
0
zachvaldezAuthor Commented:
I place this control in the masterpage. How will I access the control?
0
zachvaldezAuthor Commented:
That's it ! Glad it worked
0
zephyr_hex (Megan)DeveloperCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.