如何从ASP.NET中的前端Javascript函数调用后端C#函数

时间:2014-10-03 03:06:38

标签: javascript c# asp.net webforms

我目前正在C#和Asp.net进行无薪实习。我的雇主要求我写出一个javascript函数,以告诉用户他们是否确定在删除之前是否要从数据库中删除记录。

经过一些研究后,我能够写出Javascript函数,告诉用户他们是否确定要在从数据库中删除该记录之前删除该记录。

javascript函数有效。但是现在我遇到了如何调用后端C#函数的问题,该函数实际上会从我刚刚编写的前端javascript函数中删除数据库中的记录?

这是我的代码:

Javascript功能:

function watchdelete() 
{
    if (confirm("Are you Sure You want to delete this Manufacturer?") == true)
   {
        __doPostBack('btnDelete_Click','');//"PageMethods.btnDelete_Click();
   }
    else { }
}

调用附加到删除按钮的javascript客户端代码的前端部分:

<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick=" return watchdelete()" OnClick="btnDelete_Click1" />

后端C#函数,我想调用它来从数据库中删除记录: (请注意我会很高兴,只要我调用此功能并执行,您不必担心 关于它的内部运作太多了。谢谢)

protected void btnDelete_Click(object sender, EventArgs e)
{
    String com, command, findmodel;

    if (txtManufactureName.Text != "")                                      // If the manufacturer name is not null
    {
        if (txtManufactureName.Text == grdManufact.SelectedRow.Cells[1].Text) // And the manufacturer name must not be
        {                                                                     // Changed from selected one
            string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
            try
            {
                using (SqlConnection conn = new SqlConnection(strConnectionString))
                {
                    conn.Open();                                               // Connect to database
                    String moderated = (checkBoxModerated.Checked) ? "true" : "false";
                    findmodel = "SELECT * From VehicleModels WHERE ManufacturerID = '" + txtManID.Text + "';"; 
                    com = "SELECT * From VehicleManufacturer WHERE ManufacturerName = '" + txtManufactureName.Text + "' AND Ismoderated ='" + moderated + "';";
                    command = "DELETE From VehicleManufacturer WHERE ManufacturerName = '" + txtManufactureName.Text + "' AND Ismoderated ='" + moderated + "';";
                    SqlDataAdapter finder = new SqlDataAdapter(findmodel, conn);
                    DataTable dat = new DataTable();
                    int nummods = finder.Fill(dat);
                    if (nummods == 0)
                    {
                        SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
                        DataTable dt = new DataTable();
                        int number = adpt.Fill(dt);                                // try to find record to delete
                        if (number == 0)                            // If there is no such record to delete
                        {                                           // Indicate this to user with error message
                            txtMessage.Text = "Sorry, there is no such record to delete";
                        }
                        else
                        {                                           // Otherwise delete the record
                            using (SqlCommand sequelCommand = new SqlCommand(command, conn))
                            {
                                sequelCommand.ExecuteNonQuery();
                                txtMessage.Text = "Manufacturer Deleted Successfully";
                                txtManufactureName.Text = "";       // Reset manufacturer name
                                txtDescription.Text = "";           // Reset Description
                                checkBoxModerated.Checked = false;  // clear moderated checkbox
                            }
                        }
                    }
                    else
                    {
                        txtMessage.Text = "Sorry. You must delete associated models first.";
                    }
                    conn.Close();                               // Close the database connection. Disconnect.
                }
                BindGrid();                                     // Bind Manufacturer Grid again to redisplay new status.
            }
            catch (SystemException ex)
            {
                txtMessage.Text = string.Format("An error occurred: {0}", ex.Message);
            }
        }
        else
        {
            txtMessage.Text = "Sorry. You cant change the manufacturer name before deleting";
        }
    }
    else
    {   // Otherwise give error message if manufacturer name missing
        txtMessage.Text = "Please enter a manufacturer name to delete";
    }
}

任何想法都将受到赞赏。

2 个答案:

答案 0 :(得分:3)

让我们将验证功能简化为:

function confirmDelete(){
  return confirm("Are you Sure You want to delete this Manufacturer?");
}

然后,具有OnClientClick属性的按钮将类似于

OnClientClick=" return confirmDelete()"  

只要您的验证函数返回false,.NET就不会将您的代码提交给服务器。

答案 1 :(得分:0)

让人们了解如何从客户端调用后端函数Javascript我想用我自己的话来回答这个问题 - &gt;简单朴素的英语: 步 1.编写你的Javascript函数并将其放在客户端并启用脚本,不要在这里详细说明。请参阅下面的代码段以获取示例

   <script type = "text/javascript" >
    function watchdelete() 
    {
        return confirm("Are you Sure You want to delete this Manufacturer?");
    }
  1. 编写您的按钮或控制前端代码,并确保您的OnClientClick =您要调用的javascript函数的名称加上前面的单词return,如示例asp中所示 代码显示在原帖中。

  2. 确保按照惯例启动后端C#功能,例如双击Visual Studio 2012或2013的设计视图中的按钮或控件或其他任何方式,以便在后面的代码中自动构建其后端功能页面和代码你的后端功能显然你想要它做什么。

  3. 当你正确完成第3步后,你应该有OnClick =你的后端C#功能。

  4. 测试您的申请。一旦你的前端javascript返回true,它应该会自动启动 您的后端函数,只要您根据原始帖子中显示的前端代码或类似信息放置了一个return语句。

  5. 只要你想要它,生活就会变得简单。选择是你的。