如何在中添加参数 [英] How to add an argument in the
本文介绍了如何在中添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
参考 http:/ /www.aspsnippets.com/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx [ ^ ],我修改了代码并在JS函数中添加一个参数,以便从确认对话框中捕获是或否值。相关代码如下所示:
Referring to http://www.aspsnippets.com/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx[^], I revised the code and added an argument in the JS function in order to catch the "Yes" or "No" value from the Confirmation dialog. The related code are shown below:
// Code-behind
protected void Button1_Click(object sender, EventArgs e) {
int d = 10;
string confirmValue = Request.Form["confirm_value('" + d + "'"];
if (confirmValue == "Yes") {
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
} else {
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
// JavaScript
function Confirm(days) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "Your Password is to be expired in " + days + " days. Do you want to reset your Password?"";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
// HTML
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick = "Confirm(d)" />
但是,我得到了Microsoft JScript运行时错误:'d'未定义。怎么调试呢?非常感谢。
However, I got Microsoft JScript runtime error: 'd' is undefined. How can this be debugged? Thanks a lot.
推荐答案
当点击Button1时,你告诉浏览器调用一个名为Confirm的函数,你告诉它传递变量d。但你永远不会定义d是什么。
有几件事:
1.不要使用C#来创建JavaScript。只需将您的JavaScript写入JS文件并将其包含在您的页面中即可。它使事情更容易处理。
2.不要将函数命名为内置函数。小写确认是一个内置的JS函数,并且会混淆其他需要稍后维护代码的开发人员。
3.你的Button1基本上有2个事件。 1将在JavaScript中首先触发,然后在C#中随后触发另一个。同样,不要为此目的使用C#。只需在JavaScript中执行即可。 C#应该用于您需要在服务器上执行的操作,例如访问数据库,发送电子邮件等。
When Button1 is clicked you told the browser to call a function named Confirm and you are telling it to pass the variable d. But you never define what d is.
A couple of things:
1. Don't use C# to create JavaScript. Just write your JavaScript in a JS file and include it in your page. It keeps things much easier to deal with.
2. Don't name a function the same as a built-in function. Lowercase confirm is a built-in JS function and will confuse other developers who have to later maintain your code.
3. You basically have 2 events on your Button1. 1 that will fire first in JavaScript and then another that will happen afterwards in C#. Again, don't use C# for this purpose. Just do it in JavaScript. C# should be used for things you need to do on the server, such as access a database, send email, etc.
// Code-behind
protected void Button1_Click(object sender, EventArgs e) {
int d = 10;
// **** Changed Request.Form code ****
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes") {
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
} else {
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
// JavaScript
function Confirm(days) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
// **** Changed name ****
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
// HTML
<!-- **** Hard coded param **** -->
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick = "Confirm(10)" />
这篇关于如何在中添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文