JS、C# 和 ASP.NET 中的 getElementById().value
我尝试让 C# 和 JS 通过隐藏输入相互通信。我查看了各种论坛和代码,但似乎都没有这个确切的问题。其中许多都存在语法错误,我已尝试对其进行调试。主要是这条线
document.getElementById("Hidden1").value = str;
似乎不起作用。我尝试在该行之前和之后放置警报消息。将警报放在前面会导致警报弹出,但将其放在后面不会导致警报弹出,这让我更加怀疑这行代码。当我通过 Chrome 浏览器在 Visual Studio 2010 中运行它时,没有显示错误消息。如果有人能提供帮助,我们将不胜感激。
如果我删除更新面板,它仍然无法工作。这实际上是我一开始尝试的,但我认为可能是在单击按钮期间刷新网站时发生的,所以我尝试实现 AJAX updatepanel 属性。
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# 代码
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Hidden1.Value);
}
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
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.
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.
ASP.NET CODE
<%@ 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题可能是因为 Hidden1 的 asp.net 控件 id 和客户端 id 不同。查看 HTML 源代码以确保确定,但我认为您需要类似以下内容:
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:
客户端元素不会有 id“Hidden1”,它将有一个由 ASP.NET 自动生成的 id。
这就是为什么您在客户端遇到问题:
document.getElementById('Hidden1')
将返回null
,然后您尝试访问 <该null
引用的 code>value 属性。您需要通过
ClientID
属性:The client-side element won't have the id "Hidden1", it'll have an id that's auto-generated by ASP.NET.
That's why you're getting the problem on the client-side:
document.getElementById('Hidden1')
will returnnull
, and you're then trying to access thevalue
property of thatnull
reference.You need to use the auto-generated id in your JavaScript instead, via the
ClientID
property: