我正尝试使用以下代码
从服务器端使用是或否选项引发警报消息如果我选择确定按钮,我可以通过确定或取消按钮获取警报我需要将相同的消息(确定或取消)传回服务器
这是我背后的代码
protected void chkboxSelectAll_CheckedChanged(object sender,EventArgs e)
{
if(productNames.Any(s => s.Contains("Virtual")))
{
string str = "you have selected this type of do u want to create a new name for this?";
Page.ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "ConfirmApproval('" + str + "');", true);
string confirmationValue = Request.Form["confirm_valuetype"];
if(confirmationValue == "Yes")
{
// No able to come inside this function
ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
ddlClpFriendlyName.Items.FindByText("Create New");
FriendlynameCle_panel.Visible = true;
}
}
}
这是我的客户端功能
function ConfirmApproval(objMsg) {
var confirm_valuetype = document.createElement("INPUT");
confirm_valuetype.type = "hidden";
confirm_valuetype.name = "confirm_valuetype";
if (confirm(objMsg)) {
alert(objMsg); // i am able to get this alert
confirm_valuetype.value = "Yes";
}
else {
confirm_valuetype.value = "No";
}
document.forms[0].appendChild(confirm_valuetype);
}
我不知道为什么我无法将确认值发送回服务器端代码。 如果我出错了会非常感谢我
,请帮忙提前致谢
答案 0 :(得分:1)
您可以按照以下方式进行AJAX通话:Calling Server Side function from Client Side Script
你必须通过ajax回拨到服务器页面,这将解决你的问题
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ConfirmApproval(objMsg) {
var confirm_valuetype = document.createElement("INPUT");
confirm_valuetype.type = "hidden";
confirm_valuetype.name = "confirm_valuetype";
if (confirm(objMsg)) {
alert(objMsg); // i am able to get this alert
confirm_valuetype.value = "Yes";
}
else {
confirm_valuetype.value = "No";
}
callServer(confirm_valuetype);
}
function callServer(val) {
$.ajax({
type: "POST",
url: "PAgeName.aspx/AlertReply",
data: '{name: "' + val + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
Server code
[System.Web.Services.WebMethod]
public static string AlertReply(string name)
{
///get value and do processing
}
我认为问题是
string str = "you have selected this type of do u want to create a new name for this?";
Page.ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "ConfirmApproval('" + str + "');", true);
使用此行,一旦您的整页在客户端加载,代码就会被执行。这意味着在完成C#代码执行后创建了隐藏字段。
这意味着你这部分代码
string confirmationValue = Request.Form["confirm_valuetype"];
if(confirmationValue == "Yes")
{
// No able to come inside this function
ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
ddlClpFriendlyName.Items.FindByText("Create New");
FriendlynameCle_panel.Visible = true;
}
在html页面中创建文件之前已执行。
您可以在html中加载页面后访问字段,而不是执行按钮单击。
答案 1 :(得分:1)
将逻辑拆分为两种不同的方法。第一个(chkboxSelectAll_CheckedChanged
)将包含此部分:
if(productNames.Any(s => s.Contains("Virtual")))
{
string str = "you have selected this type of do u want to create a new name for this?";
Page.ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "ConfirmApproval('" + str + "');", true);
}
第二个(你必须编写的新方法)将包含这个:
string confirmationValue = Request.Form["confirm_valuetype"];
if(confirmationValue == "Yes")
{
ddlClpFriendlyName.Items.Add(new ListItem("Create New"));
ddlClpFriendlyName.Items.FindByText("Create New");
FriendlynameCle_panel.Visible = true;
}
我会解释。简而言之,客户端(浏览器)和服务器是两个独立的断开连接的世界。看起来你错过了这一点。
这是实际发生的事情:
chkboxSelectAll_CheckedChanged
(当然在服务器上运行)chkboxSelectAll_CheckedChanged
要求在响应中包含一个脚本,该脚本将在客户端上执行ConfirmApproval(...)
。chkboxSelectAll_CheckedChanged
方法退出(这很重要)chkboxSelectAll_CheckedChanged
包含)。 ConfirmApproval(...)
,当然在客户端上执行ConfirmApproval
对浏览器的DOM进行了一些更新。chkboxSelectAll_CheckedChanged
在很久以前就已经结束了它的执行(就网络服务器性能而言)。因此,为了让服务器处理用户的响应,您必须从客户端向服务器发出附加请求,并在服务器上处理它: