[英]Setting the value of an input through javascript
What I'm trying to do is to change a behind-the-code C# veriable's value through javascript.我想要做的是通过 javascript 更改代码背后的 C# veriable 值。 My code:
我的代码:
HTML & JS - 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# - 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);
}
So when the client first enters the site, they'll press on either background
or background2
which are supposed to set a value to the hidden input, which will be used in the registration function behind the code.因此,当客户端第一次进入站点时,他们将按下
background
或background2
,它们应该为隐藏输入设置一个值,该值将在代码后面的注册功能中使用。 When I set a breakpoint, I saw the value always was just an empty string - ""
throughout all my iterations (tried setting a To.String()
to it in C#, and removing the "" around the value in javascript, and nothing worked).当我设置断点时,我看到该值始终只是一个空字符串 -
""
在我所有的迭代中(尝试在 C# 中To.String()
设置To.String()
,并在 javascript 中删除该值周围的 "",什么也没有)工作)。 I can't figure out why it won't set a value...我不明白为什么它不会设置一个值......
Thanks for any help!谢谢你的帮助!
Maybe your error is in the chg() function, check it.也许你的错误在 chg() 函数中,检查它。
On the other hand you must add "" to values like this:另一方面,您必须将 "" 添加到这样的值中:
function SetPostbackValues() {
document.getElementById("hiddenControl").value = "Employer";
}
function SetPostbackValues2() {
document.getElementById("hiddenControl").value = "Employee";
}
Found the solution, it was the ID...找到了解决办法,是ID...
In your code, the ASP elements' ID is server-side.在您的代码中,ASP 元素的 ID 是服务器端的。 But when you run the code, the ASP element's ID in the client-side is different.
但是当您运行代码时,客户端中 ASP 元素的 ID 是不同的。 In the javascript script, the functions are looking for an element matching the server-side ID, which they will never find, hence why the hidden element's value will stay an empty string.
在 javascript 脚本中,函数正在寻找与服务器端 ID 匹配的元素,但它们永远找不到,因此隐藏元素的值将保持空字符串。
TLDR - white it like this to prevent the script from detecting a (seemingly) nonexistent element in the client-side's code: TLDR - 像这样白化它以防止脚本检测到客户端代码中(看似)不存在的元素:
function SetPostbackValues() {
document.getElementById('<%= hiddenControl.ClientID %>').value = "Employer";
}
function SetPostbackValues2() {
document.getElementById('<%= hiddenControl.ClientID %>').value = "Employee";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.