C# 如何获取客户端确认框值以进行服务器端操作?
我正在为确认消息框使用客户端脚本,如下所示C# 如何获取客户端确认框值以进行服务器端操作?,c#,javascript,asp.net,telerik,C#,Javascript,Asp.net,Telerik,我正在为确认消息框使用客户端脚本,如下所示 string script = "fadeScript"; ScriptManager.RegisterClientScriptBlock(this.Page, script.GetType(), "Script", "Closewindow();", true); java脚本函数: <script type="text/javascript"> function Closewindow() {
string script = "fadeScript";
ScriptManager.RegisterClientScriptBlock(this.Page, script.GetType(), "Script", "Closewindow();", true);
java脚本函数:
<script type="text/javascript">
function Closewindow() {
var Result = confirm("Are you sure want to delete?");
alert(Result);
if (Result == true) {
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
alert(txtConfirmresult.value);
return true;
}
else
{
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
alert('BYE');
return false;
}
}
</script>
函数Closewindow(){
var Result=confirm(“您确定要删除吗?”);
警报(结果);
如果(结果==真){
document.getElementById('txtConfirmresult')。value=Result;//分配给隐藏文本框
警报(txtConfirmresult.value);
返回true;
}
其他的
{
document.getElementById('txtConfirmresult')。value=Result;//分配给隐藏文本框
警惕(“再见”);
返回false;
}
}
如果返回true,我想使用.cs文件中的脚本返回值来激发过程。如果返回false,我只能停留在特定页面上。请在这方面帮助我您可以使用
\u doPostBack(目标,参数)
发回服务器。然后,您可以在post数据中计算\uuuu EVENTTARGET
和\uuuu EVENTARGUMENT
,以查看发送回的内容,并适当地执行逻辑
一个简单的例子:
脚本/客户端:
<script type="text/javascript">
function Closewindow() {
var Result = confirm("Are you sure want to delete?");
alert(Result);
if (Result == true) {
var txtConfirmResult = document.getElementById('txtConfirmresult');
txtConfirmResult.value = Result;//assigning to hidden text box
alert(txtConfirmresult.value); //displaying for debug purposes
__doPostBack( 'txtConfirmresult', Result ); //sending back to server.
return true;
}
else
{
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
alert('BYE');
return false;
}
}
<script type="text/javascript">
function Closewindow() {
var Result = confirm("Are you sure want to delete?");
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
if (Result) {
__doPostBack( 'UserConfirmedFormSubmission', "CloseWindow" ); //sending back to server.
return true;
}
else
{
return false;
}
}
更新后添加了稍微正确的变量名,但如果脚本正常工作,则应使用现有的代码库。我会非常小心地使用txtConfirmresult
作为您的ID,因为只有在文本框中未设置runat=“server”
时,该ID才会起作用。如果是这种情况,则ID的前面将加上表示容器层次结构的前缀
我建议您很好地命名“回调”,例如:
脚本/客户端:
<script type="text/javascript">
function Closewindow() {
var Result = confirm("Are you sure want to delete?");
alert(Result);
if (Result == true) {
var txtConfirmResult = document.getElementById('txtConfirmresult');
txtConfirmResult.value = Result;//assigning to hidden text box
alert(txtConfirmresult.value); //displaying for debug purposes
__doPostBack( 'txtConfirmresult', Result ); //sending back to server.
return true;
}
else
{
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
alert('BYE');
return false;
}
}
<script type="text/javascript">
function Closewindow() {
var Result = confirm("Are you sure want to delete?");
document.getElementById('txtConfirmresult').value = Result;//assigning to hidden text box
if (Result) {
__doPostBack( 'UserConfirmedFormSubmission', "CloseWindow" ); //sending back to server.
return true;
}
else
{
return false;
}
}
谢谢你的回复,托雷斯,你能给我解释一下如何用一个例子吗?OK,托雷斯,当我在按钮点击事件中写这篇文章时,它工作得很好,没有任何建议。你能提供更多的信息吗?你想做什么?如果后续问题和链接不相关,您可以询问后续问题和链接,或者更新当前问题以包含您正在使用的click事件处理程序代码?