【问题标题】:not able to get value from aspx page to aspx.cs无法从 aspx 页面获取值到 aspx.cs
【发布时间】:2013-10-20 20:13:22
【问题描述】:

这是我的 javascript 函数,其中值来自其他页面,并且可以完美接收,但是如何在 cs 页面中检索“divConversation”值。 这是我的代码

 function myLoad() {

        document.getElementById('divConversation').innerText = getParameterByName("id");


    }

【问题讨论】:

  • 您的 div 需要具有 runat=server 属性。然后你可以从服务器端访问它
  • @codeSpy 我尝试过使用 hiddenfield,但问题仍然存在,我如何将 hiddenfield 的值从 aspx 页面获取到 aspx.cs 页面???
  • @codeSpy 请粘贴完整的代码,因为它不起作用,无论如何我都试过了,这将是一个很大的帮助

标签: c# javascript asp.net function


【解决方案1】:

您要求的主要问题是首先执行服务器端代码,因此您需要在 page_load 上接收传递的值,而不是在 aspx 页面上接收。

这可以通过

来完成
String passedValue=Request.QueryString["id"] as string;

【讨论】:

    【解决方案2】:

    这是一个非常简单的演示:

    aspx/标记;这将在您键入时设置隐藏字段的值

    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
    <script>
        document.getElementById('<%= TextBox1.UniqueID %>').onkeyup = function (evt) {
            document.getElementById('<%= HiddenField1.UniqueID %>').value = document.getElementById('<%= TextBox1.UniqueID %>').value;
        }
    </script>
    

    代码隐藏 (.cs)

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(HiddenField1.Value);
    }
    

    【讨论】:

    • 但我可能需要在这里投射
    • document.getElementById('').value =getParameterByName("id");或 document.getElementById('').value =getParameterByName("id");还是别的什么??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2023-03-26
    • 2012-06-25
    • 2018-07-28
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多