Shruti
Shruti

Reputation: 33

create c# code of onclick function in aspx.cs

What I'm trying to do is to call a function in aspx.cs from aspx file.

JavaScript code: (for which I'm trying to make c# function in aspx.cs file)

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# code in aspx.cs: (This is what I'm trying out and getting a error)

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!!";
            }
    }
}
}

calling it from aspx file:

<asp: Button ID="Button3" runat="server" Text="SUBMIT" OnClick="myconfirm"></asp: Button>

error I'm getting

'myconfirm1' is undefined"

Summarising it:

  1. DEFINE A ONCLICK METHOD IN ASPX.CS FILE from c#.
  2. calling it from aspx file.

Problems occurring:

  1. lack of basic concept of how to exactly write the c# code in aspx.cs file.

And also If anyone can give a brief concept of how to write c# code in aspx.cs to be called from aspx file.

Upvotes: 1

Views: 5575

Answers (3)

Anand
Anand

Reputation: 165

If I am not wrong you want to eliminate your JS code and do the logic in Code Behind File.

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.
    }

Upvotes: 0

Dhaval Patel
Dhaval Patel

Reputation: 7601

you can use the below mentioned code

 <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()"/>

and your cs file look like

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);
    }
}

Upvotes: 1

Min Hong Tan
Min Hong Tan

Reputation: 545

If i'm not wrong. see below can help you up or not.

<script type="text/javascript">
        function Confirmation() {
            var rowQuestion = confirm("Are you sure you want to confirm?");
            if (rowQuestion == false) {
                return false;
            }
            return true;
        }
    </script>

and the aspx will be:

<asp:Button Text="Confirm Training" OnClientClick="return Confirmation();" ID="btnConfirm"  OnClick="btnConfirm_Click" runat="server" />

confirm at the javascript "confirmation" first, then, only call the btnConfirm_click code behind.

Upvotes: 0

Related Questions