c#之在aspx.cs中创建onclick函数的c#代码
程序猿
阅读:36
2025-12-25 22:24:30
评论:0
我想做的是从 aspx 文件调用 aspx.cs 中的函数。
JavaScript 代码:(我正在尝试在 aspx.cs 文件中创建 C# 函数)
function myconfirm() {
var txt;
var x = confirm("confirmation!!");
if (x == true) {
txt = " Your Request is Submitted!";
}
else {
txt = "Verify and Confirm!";
}
document.getElementById("verify").innerHTML = txt;
return false;
}
aspx.cs 中的 c# 代码:(这是我正在尝试并收到错误的代码)
namespace TESTING
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void myconfirm(object sender, EventArgs e)
{
string x = "confirmation!!";
string txt;
if (x == "true")
{
txt = "your request is submitted";
}
else
{
txt = "verify and confirm ";
}
}
public void myconfirm1(object sender, EventArgs e)
{
string y="confirmation!!";
string text;
if(y=="true")
{
text="we are tracking your PRN";
}
else{
text="verify and confirm!!";
}
}
}
}
从 aspx 文件调用它:
<asp: Button ID="Button3" runat="server" Text="SUBMIT" OnClick="myconfirm"></asp: Button>
我收到错误
'myconfirm1' is undefined"
总结:
- 在 c# 的 ASPX.CS 文件中定义 ONCLICK 方法。
- 从 aspx 文件调用它。
出现问题:
- 缺乏如何在 aspx.cs 文件中准确编写 C# 代码的基本概念。
还有如果有人可以给出如何在 aspx.cs 中编写从 aspx 文件调用的 C# 代码的简要概念。
请您参考如下方法:
您可以使用下面提到的代码
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
你的cs文件看起来像
public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



