source to code behind

zachvaldez
zachvaldez used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
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?

Author

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 " ".
Software Team Lead
Commented:
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

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Dirk StraussSenior Full Stack Developer
Distinguished Expert 2017

Commented:
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.

Author

Commented:
I place this control in the masterpage. How will I access the control?

Author

Commented:
That's it ! Glad it worked
Top Expert 2010

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial