简体   繁体   English

ASP.NET在Javascript中将hiddenfield设置为一个值

[英]ASP.NET set hiddenfield a value in Javascript

I don't know how to set the value of a hiddenField in Javascript. 我不知道如何在Javascript中设置hiddenField的值。 Can somebody show me how to do this? 有人可以告诉我该怎么做吗?

Javascript: Javascript:

document.getElementById('hdntxtbxTaksit').value = "";

HTML: HTML:

<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false">   </asp:HiddenField>

error : "Unable to get value of the property \\'value\\': object is null or undefined" 错误:“无法获取属性\\'value \\'的值:对象为null或未定义”

Prior to ASP.Net 4.0 在ASP.Net 4.0之前

ClientID 客户编号

Get the client id generated in the page that uses Master page. 获取在使用“母版”页面的页面中生成的客户端ID。 As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like ctrl_ . 由于母版页为UserControl类型,因此它将具有自己的ID,并将该页视为子控件,并生成带有ctrl_前缀的其他ID。

This can be resolved by using <%= ControlName.ClientID %> in a page and can be assigned to any string or a javascript variables that can be referred later. 可以通过在页面中使用<%= ControlName.ClientID %>来解决此问题,并且可以将其分配给以后可以引用的任何字符串或javascript变量。

var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
  • Asp.net server control id will be vary if you use Master page. 如果您使用母版页,则Asp.net服务器控件ID将有所不同。

ASP.Net 4.0 + ASP.Net 4.0以上

ClientIDMode Property ClientIDMode属性

Use this property to control how you want to generate the ID for you. 使用此属性可以控制您如何为您生成ID。 For your case setting ClientIDMode="static" in page level will resolve the problem. 对于您的情况,在页面级别设置ClientIDMode =“ static”将解决此问题。 The same thing can be applied at control level as well. 同样的事情也可以应用于控制级别。

asp:HiddenField as: asp:HiddenField为:

<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />

js code: js代码:

$("#hfProduct").val("test")

and the code behind: 以及后面的代码:

hfProduct.Value.ToString();

试试这个代码:

$('hdntxtbxTaksit').val('test');
  • First you need to create the Hidden Field properly 首先,您需要正确创建隐藏字段

    <asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>

  • Then you need to set value to the hidden field 然后您需要将值设置为隐藏字段

    If you aren't using Jquery you should use it: 如果您不使用Jquery ,则应该使用它:

    document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";

    If you are using Jquery , this is how it should be: 如果使用的是Jquery ,则应该这样:

    $("#<%= hdntxtbxTaksit.ClientID %>").val("test");

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

您在服务器中设置的ID是与客户端ID不同的服务器ID。

My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. 我的理解是,如果在初始页面加载期间设置了controls.Visible = false ,则不会在客户端响应中呈现它。 My suggestion to solve your problem is 我建议解决您的问题是

Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. 从场景来看,不要使用占位符,除非您需要在服务器端动态添加控件,否则您实际上不需要占位符。 Use div, without runat=server . 使用div,不带runat=server You can always controls the visiblity of that div using css. 您始终可以使用CSS控制该div的可见性。 If you need to add controls dynamically later, use placeholder, but don't set visible = false . 如果以后需要动态添加控件,请使用占位符,但不要设置visible = false Placeholder won't have any display anyway, Set the visibility of that placeholder using css. 无论如何,占位符将不会显示,请使用css设置该占位符的可见性。 Here's how to do it programmactically : 这是通过编程方式进行的方法:

placeholderId.Attributes["style"] = "display:none";

Anyway, as other have stated, your problems occurs because once you set control.visible = false , it doesn't get rendered in the client response. 无论如何,就像其他人所说的那样,您的问题之所以发生是因为一旦您设置control.visible = false ,它就不会在客户端响应中呈现。

I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you've marked the field as runat="server" . 我怀疑您需要在JavaScript代码中使用ClientID而不是文字ID字符串,因为您已将该字段标记为runat="server"

Eg, if your JavaScript code is in an aspx file (not a separate JavaScript file): 例如,如果您的JavaScript代码在aspx文件(而不是单独的JavaScript文件)中:

var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;

If it's in a separate JavaScript file that isn't rendered by the ASP.Net stuff, you'll have to find it another way, such as by class. 如果它位于一个单独的JavaScript文件中,而该文件不是由ASP.Net呈现的,则必须另找它,例如按类。

I will suggest you to use ClientID of HiddenField. 我建议您使用HiddenField的ClientID。 first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. 首先,从代码隐藏后的任何Javascript变量中注册其客户端ID,然后在客户端脚本中使用它。 as: 如:

.cs file code: .cs文件代码:

ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);

and then use following code in JS: 然后在JS中使用以下代码:

document.getElementById(hdntxtbxTaksit).value= ""; 

尝试将Javascript值设置为document.getElementByName('hdntxtbxTaksit').value = '0';

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

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