JS、C# 和 ASP.NET 中的 getElementById().value

发布于 2024-12-14 01:01:41 字数 1882 浏览 5 评论 0原文

我尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-12-21 01:01:41

您的问题可能是因为 Hidden1 的 asp.net 控件 id 和客户端 id 不同。查看 HTML 源代码以确保确定,但我认为您需要类似以下内容:

document.getElementById("<%= Hidden1.ClientID %>").value = "Some value";

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:

document.getElementById("<%= Hidden1.ClientID %>").value = "Some value";
梦初启 2024-12-21 01:01:41

客户端元素不会有 id“Hidden1”,它将有一个由 ASP.NET 自动生成的 id。

这就是为什么您在客户端遇到问题:document.getElementById('Hidden1') 将返回 null,然后您尝试访问 <该 null 引用的 code>value 属性。

您需要通过 ClientID 属性:

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

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 return null, and you're then trying to access the value property of that null reference.

You need to use the auto-generated id in your JavaScript instead, via the ClientID property:

document.getElementById('<%=Hidden1.ClientID%>').value = str;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文