简体   繁体   English

来自JavaScript的HiddenField始终为NULL

[英]HiddenField always NULL, from javascript

I created a Context menu and associated to a Gridview, when right click is performed.The problem is that when a Press a button from that context menu, I want to get the value from a HiddenField. 当右键单击时,我创建了一个上下文菜单并关联到Gridview。问题是,当从该上下文菜单中按一个按钮时,我想从HiddenField获取值。 But It always returns null. 但是它总是返回null。 I've been reading, And I think It might be because The DOM is not fully loaded, But I could not resolve It using $( document ).ready(). 我一直在阅读,并且我认为可能是因为DOM尚未完全加载,但是我无法使用$(document).ready()来解析它。 Probably using It the wrong way. 可能以错误的方式使用它。

Let me know if You have any Ideas 让我知道您是否有任何想法

HTML: HTML:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="../../../Scripts/PendientesDataGridContextMenu.js"></script>
<link href="../../../Styles/ContextMenu.css" rel="stylesheet" type="text/css" />

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<asp:HiddenField ClientIDMode="static" runat="server" ID="hdnSomeField" />
<asp:HiddenField ClientIDMode="static" ID="fldNumeroCQCallTx" runat="server"  />
<asp:HiddenField ID="fldNumeroCQMalaPractica" runat="server"  />

<div id="myMenu" class="contextMenu">
<table style='width:100%;'>
<tr><td onclick="fnCargarfldNumeroCQCallTxYClickear();">Registrar</td></tr>
<tr><td onclick="fnDelete();">Registrar2</td></tr>
</table>
</div>

<asp:LinkButton ID="lnkCallTx" runat="server"
style="display:none" OnClick="lnkCallTx_Click" />

C# Code: C#代码:

protected void Page_Load(object sender, EventArgs e)
    {
        //Associate Context Menu to Gridview
        Page.ClientScript.RegisterStartupScript(GetType(), "Script", "fnLinkearContextMenu(); ", true);

    }

Java Script: Java脚本:

function fnLinkearContextMenu() {

    $("#myMenu").hide();

    $("table[id$='cntMainPlaceHolder_MainContent_PendientesGestionDeTareasDataGridView'] > tbody > tr").bind('contextmenu', function (e) {
        $("#myMenu").hide();
        e.preventDefault();
        $("#myMenu").css({
            top: e.pageY + "px",
            left: e.pageX + "px",
            position: 'absolute'
        });

        //document.getElementById('<%= lnkCallTx.ClientID %>').value = rowid;
        $("#myMenu").show();

    });

    //Cuando realizan click izquierdo en otra parte de la pagina

    $(document).bind('click', function (e) {
        $("#myMenu").hide();
    });

};

function fnCargarfldNumeroCQCallTxYClickear() {

    var lnkView = document.getElementById('<%=lnkView.ClientID %>').value;
    var lnkCallTx = document.getElementById('lnkCallTx');

    lnkCallTx.click();
};

The reason you are unable to get a value out of the hidden field is related to server tags. 无法从隐藏字段中获取值的原因与服务器标签有关。 In this case, <% %> are referred to as server tags so they have to hit the code behind (aspx.cs file) in order to resolve the value that's passed in. In a .js file, there is no code behind, so instead of resolving the value, it's just putting the static string in the output script. 在这种情况下,<%%>被称为服务器标记,因此它们必须击中后面的代码(aspx.cs文件)才能解析传入的值。在.js文件中,没有代码,因此,与其解析该值,不如将静态字符串放入输出脚本中。

script: 脚本:

document.getElementById('<%= lnkView.ClientID %>').value;

.aspx file output: .aspx文件输出:

document.getElementById('cntMainPlaceHolder_MainContent_lnkView').value;

.js file output: .js文件输出:

document.getElementById('<%= linkView.ClientID %>').value;

To prove this you can open the code inspector in the browser you're working in (f12 most likely) and look at your included script. 为了证明这一点,您可以在使用的浏览器中打开代码检查器(很可能是f12),然后查看包含的脚本。 You will see that there are <% tags in there which should have been resolved out. 您将看到其中有<%标记应已解决。 This same concept will apply to JQuery as well: 同样的概念也将适用于JQuery:

$('#<%= lnkView.ClientID %>').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM