如何从母版页上的链接获取文本? [英] How to get the text from an link on the master page?

查看:32
本文介绍了如何从母版页上的链接获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版页,上面有几个链接,不是 asp:Hyperlinks,只是普通的标签.这些链接位于页面顶部的菜单栏上.

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?

我使用的是 .net 网络表单.

I'm using .net web forms.

推荐答案

您可以使用 jQuery 访问母版页中的元素.

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>

由于母版页和子页在 (document).ready 方法完成之前呈现,因此可以确保构建到最终页面上的所有元素都是可见的.

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.

你只需要确保你有一个 jQuery 链接/引用:

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>

编辑 #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();

这篇关于如何从母版页上的链接获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆