ailinmcc666
ailinmcc666

Reputation: 413

How to get the text from an link on the master page?

I have a master page with a few links on it, not asp:Hyperlinks, just normal tags. The links are on a menu bar that runs along the top of the page.

Then on the child page, when I click a button, I want to be able to get the value of a specific link on the menu bar at the top of the screen on the code behind page.

Does anyone know if I can do this, and if so, how?

I'm using .net web forms.

Upvotes: 0

Views: 103

Answers (1)

user6911980
user6911980

Reputation:

You can use jQuery to access elements within the master page.

<script>
    $(document).ready(function () {
        //Some function for someID on your master page:
        $("#someID").toggle();
    });
</script>

Since the master page and child pages are rendered before the (document).ready method completes, it ensures that all elements built onto the final page are visible.

Placing the above script into your child page will allow you to access elements in the master page file.

You will just need to ensure you have a jQuery link/reference:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> 
    </script>
</head>

EDIT #1:

For getting the text off the master page into the code-behind of the child page you can do this (add hidden field to child page):

<asp:HiddenField ID="hdField" Value="SomeValue" runat="server" />

<script>
    $(document).ready(function () {
        //Some function for someID on your master page:
        $("#hdField").value = ("#IDofLinkOnMasterPage").Value;
    });
</script>

Then when your form posts to the child code-behind, you can look for the value of the hidden field by doing this:

var x = hdField.Value.ToString();

Upvotes: 1

Related Questions