javascript JS、C# 和 ASP.NET 中的 getElementById().value
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004579/
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
getElementById().value in JS, C#, and ASP.NET
提问by user1028998
I tried to get C# and JS to communicate each other through a hidden input. I looked a various forums and codes, but none of them seems to have this exact problem. Many of them had syntax error, and I have tried to debug that. It's mainly the line
我试图让 C# 和 JS 通过隐藏的输入相互交流。我查看了各种论坛和代码,但似乎没有一个有这个确切的问题。他们中的许多人都有语法错误,我试图对其进行调试。主要是这条线
document.getElementById("Hidden1").value = str;
that doesn't seem to be working. I have tried putting alert messages before and after that line. Putting the alert before causes the alert to pop up, but putting it after doesn't cause it to pop-up which makes me suspect this line of code even more. No error messages were displayed when I ran it in Visual Studio 2010 through Chrome browser. If anyone can help with it at all, it'd be greatly appreciated.
这似乎不起作用。我尝试在该行之前和之后放置警报消息。将警报放在之前会导致警报弹出,但将其放在之后不会导致它弹出,这让我更加怀疑这行代码。当我通过 Chrome 浏览器在 Visual Studio 2010 中运行它时,没有显示错误消息。如果有人可以提供帮助,将不胜感激。
If I remove the updatepanels, it still wouldn't work. It was actually what I tried at first, but I thought it might've been when the site refreshed during the button click, so I tried implementing the AJAX updatepanel property.
如果我删除更新面板,它仍然无法正常工作。这实际上是我最初尝试的,但我认为它可能是在单击按钮期间刷新站点时发生的,所以我尝试实现 AJAX updatepanel 属性。
ASP.NET CODE
ASP.NET 代码
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication10._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
<div>
<input id="Hidden1" type="hidden" name="Hidden1" runat="server" value="replaceme" />
<asp:Button ID="Button1" runat="server" OnClientClick="abc()" Text="Button"
onclick="Button1_Click" />
</div>
<div>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function abc() {
var str = "value";
document.getElementById("Hidden1").value = str;
}
</script>
</div>
</asp:Content>
C# CODE
代码
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Hidden1.Value);
}
回答by Sam
Your problem is probably because the asp.net control id and the client-side id for Hidden1 are not the same. View the HTML source to be sure, but I think you'll need something like:
您的问题可能是因为 Hidden1 的 asp.net 控件 ID 和客户端 ID 不相同。查看 HTML 源代码以确保,但我认为您需要以下内容:
document.getElementById("<%= Hidden1.ClientID %>").value = "Some value";
回答by LukeH
The client-side element won't have the id "Hidden1", it'll have an id that's auto-generated by ASP.NET.
客户端元素不会有 id“Hidden1”,它将有一个由 ASP.NET 自动生成的 id。
That's why you're getting the problem on the client-side: document.getElementById('Hidden1')
will return null
, and you're then trying to access the value
property of that null
reference.
这就是您在客户端遇到问题的原因:document.getElementById('Hidden1')
will return null
,然后您尝试访问该引用的value
属性null
。
You need to use the auto-generated id in your JavaScript instead, via the ClientID
property:
您需要通过以下ClientID
属性在 JavaScript 中使用自动生成的 id :
document.getElementById('<%=Hidden1.ClientID%>').value = str;