1

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.

2
  • string x = "confirmation!!"; and then if (x == "true") How ?? Commented May 27, 2014 at 8:19
  • I don't know just m trying out...I have written above that I m lacking basic concepts of c#...I m studying it by my own nd making A web application...I know javascript and m quite good in "inline coding" but now m trying "code behind " for same...so plz help! @DhavalPatel Commented May 27, 2014 at 8:22

3 Answers 3

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

THANK U so much its Done !!and plz if u can answer one more question from basic that "When we need to create a new Namespace and how to write it correctly??" @DhavalPatel
@Shruti:if this answers is useful then don't forgot to accept it so other can use it
I have many textboxes in my page in which I have used "range validation" and as there will be only one SUBMIT BUTTON in my page, how can I use that single button for both calling above function as well as it should also keep check over the "Range validator" i.e no fields should be left blank /invalid entries????
you have to use validation group : w3schools.com/aspnet/…
Thank u so much just two words and my problem is solved..I just had to add causevalidation="true" validationgroup="valid" in my asp:button description and its working perfectly..@DhavalPatel
|
0

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.

9 Comments

thnks for ur effort but THE above code u have given can't be written in ASPX.CS file....I m trying to do "code behind" of the javascript I have given in my question. @Tan
@Shruti: you mean you want to call javascript from code behind?
<button onclick=" return myconfirm()" SUBMIT </button><p id="verify"> </p> <script type="text/javascript"> 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; } </script></div>`
above is my javascript code which I have written in aspx file and this is working perfectly but now I want to do same thing without these javascript code and in place of it writing a c# code in aspx.cs file .I hope now my question is clear! @DhavalPatel
you mean, even javascript you want to code it dynamically/static in codebehind, so that, you no need to have two kind of maintenance one in aspx and one in aspx.cs?
|
0

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

1 Comment

I believe you are new to SO, so if you find an answer correct you mark it as correct and accept it :P Also, you need to look for C# books first to get into C# programming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.