在asp.net .cs中创建onclick函数的c#代码
本文关键字:函数 代码 onclick 创建 asp net cs | 更新日期: 2023-09-27 18:05:13
我想做的是在aspx文件中调用aspx.cs中的函数。
JavaScript代码:(为此我试图使c#函数在aspx.cs文件)
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;
}
c#代码在aspx.cs:(这是我正在尝试和得到一个错误)
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"
总结它:
- 在asp.net文件中定义一个ONCLICK方法。
- 从aspx文件中调用
问题发生:
- 缺乏在aspx.cs文件中编写c#代码的基本概念。
如果有人能给一个简单的概念,如何编写c#代码在aspx.cs从aspx文件调用
可以使用下面提到的代码
<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);
}
}
如果我没有错的话。看下面能不能帮你起来
<script type="text/javascript">
function Confirmation() {
var rowQuestion = confirm("Are you sure you want to confirm?");
if (rowQuestion == false) {
return false;
}
return true;
}
</script>
和aspx将是:
<asp:Button Text="Confirm Training" OnClientClick="return Confirmation();" ID="btnConfirm" OnClick="btnConfirm_Click" runat="server" />
首先在javascript的"确认"处确认,然后,只调用后面的btnConfirm_click代码。
如果我没有错,你想消除你的JS代码,并在代码背后文件做逻辑。
public void myconfirm(object sender, EventArgs e)
{
string x = "confirmation!!";
string txt;
if (x == "true") // your code, I have no idea why you are doing it. You have set x to confirmation so it will never be true in first place. anyways!!
{
txt = "your request is submitted";
}
else
{
txt = "verify and confirm ";
}
Verify.Text = txt; // Verify is the label where you want to display your final result.
}