【问题标题】:Setting the value of an input through javascript通过javascript设置输入的值
【发布时间】:2021-12-16 06:32:35
【问题描述】:

我要做的是通过 javascript 更改代码背后的 C# veriable 的值。我的代码:

HTML & JS -

function SetPostbackValues() {
    document.getElementById("hiddenControl").value = "Employer";
}
function SetPostbackValues2() {
    document.getElementById("hiddenControl").value = "Employee";
}
<!-- it's all inaide a form tag  -->
<div id="left">
  <div id="background" style="background: url(paper.gif); background-size: cover;" onclick="chg();SetPostbackValues();"><img id="man" style="right:16px;" src="https://www.watertankfactory.com.au/wp-content/uploads/2015/08/Smiling-young-casual-man-2.png" />    <div id="desc">employer</div>
  </div>
    <div id="trying"></div>
  </div>
  <div id="right">
    <div id="background2" style="background: url(paper.gif); background-size: cover;" onclick="chg();SetPostbackValues2();"><img id="man2" style="right:16px;" src="https://www.watertankfactory.com.au/wp-content/uploads/2015/08/Smiling-young-casual-man-2.png" /><div id="desc2">employee</div>
  </div>
</div>
    <input id="hiddenControl" type="hidden" runat="server" />

C# -

protected void RegisterUser()
        {
        //this will run when the client presses on the 'register' button
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
        u.Email = Email.Text;
        u.Password = Password.Text;
        u.Firstname = Firstname.Text;
        u.Lastname = Lastname.Text;
        u.Gender = Gender.SelectedItem.Text;
        //relevant bit:
        u.Type = hiddenControl.Value;
        u.Birthday = Birthday.SelectedDate.ToString("dd/MM/yyyy");
        u.Country = Country.Text;
        u.City = City.Text;
        u.Address = Address.Text;

        if (Pfp.HasFile)
        {
            string path = Server.MapPath("pics/");
            Pfp.SaveAs(path + Pfp.FileName);
            u.Pfp = Pfp.FileName;
        }
        s.AddClient(u);
    }

所以当客户第一次进入网站时,他们会按下backgroundbackground2,它们应该为隐藏的输入设置一个值,这将在代码后面的注册功能中使用。当我设置断点时,我看到该值始终只是一个空字符串 - 在我的所有迭代中""(尝试在 C# 中为其设置 To.String(),并在 javascript 中删除值周围的“”,但没有任何效果)。我不知道为什么它不会设置一个值...

感谢您的帮助!

【问题讨论】:

    标签: javascript c# html code-behind


    【解决方案1】:

    也许你的错误在 chg() 函数中,检查一下。
    另一方面,您必须将 "" 添加到如下值:

    function SetPostbackValues() {
        document.getElementById("hiddenControl").value = "Employer";
    }
    function SetPostbackValues2() {
        document.getElementById("hiddenControl").value = "Employee";
    } 
    

    【讨论】:

    • 我尝试使用 "" 调整代码但没有它们无济于事......这就是为什么 Idk 问题甚至可能是
    • @kfirezer 如果您使用 Razor,请尝试使用 asp-for="hiddenControl" 如果没有尝试将 name="hiddenControl" 添加到元素。
    • 我会努力更新的!编辑:即使我添加了两个属性,它仍然是一个空字符串...
    【解决方案2】:

    找到了解决办法,就是ID...

    在您的代码中,ASP 元素的 ID 是服务器端的。但是当您运行代码时,客户端中的 ASP 元素的 ID 是不同的。在 javascript 脚本中,函数正在寻找与服务器端 ID 匹配的元素,但它们永远找不到,因此隐藏元素的值将保持为空字符串。

    TLDR - 像这样编写它以防止脚本检测到客户端代码中(看似)不存在的元素:

    function SetPostbackValues() {
            document.getElementById('<%= hiddenControl.ClientID %>').value = "Employer";
        }
    function SetPostbackValues2() {
            document.getElementById('<%= hiddenControl.ClientID %>').value = "Employee";
        }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-27
      • 2013-10-14
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 2013-04-12
      相关资源
      最近更新 更多