C# ASP.NET 在 Javascript 中设置 hiddenfield 一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13454985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:37:46  来源:igfitidea点击:

ASP.NET set hiddenfield a value in Javascript

c#javascriptasp.net

提问by ozkank

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?

我不知道如何在 Javascript 中设置 hiddenField 的值。有人可以告诉我如何做到这一点吗?

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\' 的值:对象为空或未定义”

采纳答案by Murali Murugesan

Prior to ASP.Net 4.0

在 ASP.Net 4.0 之前

ClientID

客户编号

Get the client id generated in the page that uses Master page. 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_.

获取在使用母版页的页面中生成的客户端 ID。由于母版页是 UserControl 类型,它将有自己的 Id 并将页面视为子控件并生成一个不同的 id 前缀,如ctrl_.

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. For your case setting ClientIDMode="static"in page level will resolve the problem. The same thing can be applied at control level as well.

使用此属性来控制您希望如何为您生成 ID。对于您的情况,在页面级别设置ClientIDMode="static"将解决问题。同样的事情也可以应用于控制级别。

回答by T.J. Crowder

I suspect you need to use ClientIDrather than the literal ID string in your JavaScript code, since you've marked the field as runat="server".

我怀疑您需要ClientID在 JavaScript 代码中使用而不是文字 ID 字符串,因为您已将该字段标记为runat="server".

E.g., 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.

如果它位于不由 ASP.Net 内容呈现的单独 JavaScript 文件中,则必须以另一种方式找到它,例如按类。

回答by Amiram Korach

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

The id you set in server is the server id which is different from client id.

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

回答by SaiKiran Mandhala

My understanding is if you set controls.Visible = falseduring initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is

我的理解是,如果您controls.Visible = false在初始页面加载期间设置,则不会在客户端响应中呈现。我解决你的问题的建议是

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. You can always controls the visiblity of that div using css. If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :

不要使用占位符,从场景来看,其实你并不需要占位符,除非你需要在服务器端动态添加控件。使用 div,不带runat=server. 您始终可以使用 css 控制该 div 的可见性。如果稍后需要动态添加控件,请使用占位符,但不要设置visible = false. 占位符无论如何都不会显示,使用 css 设置该占位符的可见性。以下是如何以编程方式执行此操作:

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,它就不会在客户端响应中呈现。

回答by Sanjeev Rai

I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:

我建议你使用 HiddenField 的 ClientID。首先在代码隐藏的任何 Javascript 变量中注册其客户端 ID,然后在客户端脚本中使用它。作为:

.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= ""; 

回答by Jason Cole

Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';

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

回答by Black sheep

try this code:

试试这个代码:

$('hdntxtbxTaksit').val('test');

回答by Gabriel Brito

  • 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 Jqueryyou should use it:

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

    If you are using Jquery, this is how it should be:

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

  • 首先,您需要正确创建隐藏字段

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

  • 然后你需要为隐藏字段设置值

    如果您不使用Jquery,则应该使用它:

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

    如果您使用的是Jquery,它应该是这样的:

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

回答by ehsan rezaee

asp:HiddenFieldas:

asp:HiddenField作为:

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

js code:

js代码:

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

and the code behind:

以及背后的代码:

hfProduct.Value.ToString();