javascript - 通过javascript设置输入的值
问题描述
我正在尝试做的是通过 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);
}
因此,当客户第一次进入该站点时,他们将按下其中一个background
或background2
哪个应该为隐藏输入设置一个值,该隐藏输入将用于代码后面的注册功能。当我设置断点时,我看到该值始终只是一个空字符串 -""
在我所有的迭代中(尝试To.String()
在 C# 中设置它,并在 javascript 中删除值周围的“”,但没有任何效果)。我不明白为什么它不会设置一个值......
谢谢你的帮助!
解决方案
也许你的错误在 chg() 函数中,检查一下。
另一方面,您必须将“”添加到如下值:
function SetPostbackValues() {
document.getElementById("hiddenControl").value = "Employer";
}
function SetPostbackValues2() {
document.getElementById("hiddenControl").value = "Employee";
}
推荐阅读
- pyspark - Databricks 笔记本中的多选小部件
- regex - 转换href tel标签中的数字
- c# - 使用 MassTransit SignalR 背板时集线器未收到任何消息
- python - TypeError:不支持的操作数类型/:'str'和'int'---对于X_train.columns中的col:
- javascript - 平局结果无法输出
- latex - 未找到“libertineotf.sty”
- javascript - 如何将文本文件从桌面 C# 应用程序传递到 Javascript Web 应用程序
- c# - 在长时间运行的hangfire进程中发送心跳
- android - Flutter - 将数据从 Future 变量(sqlite)传递到小部件
- sql - 如何从查询中删除完全重复的行,而没有唯一值?