如何使用C#代码获取confirm()值

时间:2012-04-20 18:39:46

标签: javascript asp.net javascript-events ado.net

我有一个按钮,需要获取confirm()值才能执行数据库操作。例如:单击按钮时,会弹出一条消息确认“是或否”,如果是,它将在数据库中执行删除操作,如果不是,则清除文本框。如何从javascript confirm()函数获取返回值。请指教。感谢

string jScript;

jScript = "<script> function processConfirm(answer) {if (answer) {return 'Facilitator Deleted';}else {return 'Cancelled';}} var confirmAnswer = confirm('You sure to delete ?');var theAnswer = processConfirm(confirmAnswer);alert(theAnswer);</script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);


/* SqlCommand cmd = new SqlCommand("TrainerFilter", conn);

cmd.CommandType = CommandType.StoredProcedure;


cmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 50).Value = TextBox1.Text.Trim();


cmd.Parameters.Add("@result", SqlDbType.Int, 50).Direction = ParameterDirection.Output;


conn.Open();


cmd.ExecuteNonQuery();*/

2 个答案:

答案 0 :(得分:0)

您可以将confirm_proceed()的值存储在asp:HiddenField中 您可以按如下方式修改脚本:        function confirm_proceed()      {        var hiddenField = document.getElementById('hiddenFieldId');

   if (confirm("Are you sure you want to proceed?")==true)
   {
     hiddenField.value = 'true';
     return true;
   }
   else
   {
     hiddenField.value = 'false';
     return false;
   }
 }

答案 1 :(得分:0)

这有点令人困惑,但假设这个确认与点击按钮有关,你通常会做这样的事情:

<script type="text/javascript">
    processConfirm = function() {
        var result = confirm("Are you sure you want to delete this?");
        if (!result) {
            alert("Cancelled"); //or set a label or whatever
            return false; //cancel postback
        }
        return true; //perform postback
    }
</script>
<asp:Button ID="Button1" runat="server" OnClientClick="return processConfirm();" OnClick="Button1_Click" />

在代码隐藏中:

protected void Button1_Click(object sender, EventArgs e)
{
    //perform the delete logic since because if you reach here
    //you know that the user confirmed

    //throw up an alert when the page is reloaded confirming that the deletion
    //you could also set a label here instead of an alert if you wanted
    Page.ClientScript.RegisterStartupScript(this.GetType(), "confirmDelete", "alert('User was deleted');", true);
}