gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:35:36 26 4
gpt4 key购买 nike

我目前正在从事 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";
}
}

任何想法将不胜感激。

最佳答案

让我们将验证函数简化为:

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

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

OnClientClick=" return confirmDelete()"  

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

关于javascript - 如何从 ASP.NET 中的前端 Javascript 函数调用后端 C# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com