簡體   English   中英

在aspx.cs中創建onclick函數的C#代碼

[英]create c# code of onclick function in aspx.cs

我想做的是從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”未定義”

總結一下

  1. 在c#中的ASPX.CS文件中定義一個onclick方法。
  2. 從aspx文件調用它。

發生的問題

  1. 缺少如何在aspx.cs文件中准確編寫C#代碼的基本概念。

還有,如果有人可以簡要介紹如何在aspx.cs中編寫c#代碼,以便從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.
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM