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?
 
Ryan ChongCommented:
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.