服务器端是 否 使用 JavaScript 的确认消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23391359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server Side Yes No Confirmation Message Box using JavaScript
提问by moe
I have created a hidden field in my JavaScript and this hidden filed is used to capture the value that users select from the message box. When selection is made from the drop-down box, I am making a call to check if the ID that is selected from the drop down is already in the table, if only the ID is found in the table then I am calling the JavaScript to show the message box. If user Selects Yes show some alert and if user selects No show another alert. Somehow, I am having hard time to make it to work, nothing happens when I make selection from the drop-down. Please help as I have spent days on researching this. thanks here my code:
我在 JavaScript 中创建了一个隐藏字段,这个隐藏字段用于捕获用户从消息框中选择的值。从下拉框中进行选择时,我正在调用以检查从下拉列表中选择的 ID 是否已在表中,如果仅在表中找到该 ID,则我将调用 JavaScript显示消息框。如果用户选择是显示一些警报,如果用户选择否显示另一个警报。不知何故,我很难让它工作,当我从下拉菜单中进行选择时没有任何反应。请帮助,因为我花了几天时间研究这个。在此感谢我的代码:
//javascript code
<script type="text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to delete data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
//drop down in aspx file
//下拉到aspx文件中
//code behind
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
string ID = ddlName.SelectedValue;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT count(*) from MyTable WHERE ID =@ID";
cmd.Parameters.AddWithValue("@ID", ID);
con.Open();
int result = (int)cmd.ExecuteScalar();
if (result >= 1)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Confirm()", true);
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);
}
}
con.Close();
}
}
}
回答by David
This isn't doing what you think it's doing:
这不是在做你认为它在做的事情:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Confirm()", true);
string confirmValue = Request.Form["confirm_value"];
Server-side code runs in its entiretybefore rendering the page to the client. There's no implicit back-and-forth between the two. So the above lines basically say:
在将页面呈现给客户端之前,服务器端代码会完整运行。两者之间没有隐含的来回。所以上面几行基本上说:
When the page renders, create a form element in JavaScript.
Get the value from the form element.
Well, the page hasn't rendered yet. There is no form element. So there's no value to get at this point.
好吧,页面还没有呈现。没有表单元素。所以在这一点上没有任何价值。
As suggested in the comments, what you're looking to do is make an AJAX request from your client-side (JavaScript) code to a server-side resource (page, handler, etc.). There are lotsof tutorials available on how to use AJAX in ASP.NET. It looks like you're using WebForms, so I'd recommend either using an ASHX handler (best practice) or a standalone ASPX page (sometimes easier) to facilitate the AJAX requests.
正如评论中所建议的那样,您要做的是从客户端 (JavaScript) 代码向服务器端资源(页面、处理程序等)发出 AJAX 请求。有很多关于如何在 ASP.NET 中使用 AJAX 的教程。看起来您正在使用 WebForms,因此我建议您使用 ASHX 处理程序(最佳实践)或独立的 ASPX 页面(有时更容易)来促进 AJAX 请求。
Essentially you would want to build a JavaScript function to call in your client-side code which will initiate the AJAX call and include the form value as a parameter (either a GET or POST parameter would be easy). The server-side handler (ASHX or ASPX) would receive that parameter, check the database, and respond with a result. The result isthe content of the response (not like returning from a method), there shouldn't be any page output. The JavaScript AJAX call receives the response, checks the returned value, and responds to the user accordingly.
本质上,您希望构建一个 JavaScript 函数来调用您的客户端代码,该函数将启动 AJAX 调用并将表单值包含为参数(GET 或 POST 参数都很容易)。服务器端处理程序(ASHX 或 ASPX)将接收该参数,检查数据库,并以结果进行响应。结果是响应的内容(不像从方法返回),不应该有任何页面输出。JavaScript AJAX 调用接收响应,检查返回值,并相应地响应用户。
The main thing to remember is that there is a hard physical separation between client-side code and server-side code. They can't "call each other", they don't run simultaneously, etc. Clients make HTTP requests to servers, servers receive those requests, run some code, and respond with HTTP responses. Clients receive those responses, parse the results, and run their code accordingly. Everything happens as HTTP requests and responses.
要记住的主要事情是客户端代码和服务器端代码之间存在硬物理分离。它们不能“相互调用”,它们不能同时运行等等。客户端向服务器发出 HTTP 请求,服务器接收这些请求,运行一些代码,并以 HTTP 响应进行响应。客户端接收这些响应,解析结果,并相应地运行他们的代码。一切都以 HTTP 请求和响应的形式发生。