none
how call button onclick conditionally from C#.Net?

    Question

    1. this.button.Attributes.Add("onclick","return confirm('Are you sure you want to change?');")
    2. the above code only work in pageload(),
    3. I do not want to call onlick every time when I click button, I want to check first, if you change something in textbox, I want to call onlick, I put the code inside the program, it does not work.
    4. how can I call onlick javascript conditionally from C#.Net? Thank you!
    Monday, January 28, 2008 5:44 PM

Answers

  • Hi,

     

    For your concern, you can try to refer to the following sample codes:

    Code Snippet

    protected void btnTest_Click(object sender, EventArgs e)
        {
            bool bChanged = HasChanged();
            if (bChanged)
            {
               
    Response.Write("<script language=javascript>confirm(\"Are you sure to change?\");</script>");
            }
        }

     

        //Determine if you change something in a TextBox

        private bool HasChanged()
        {
            return true;
        }

     

    Hope this helps,

    Wednesday, January 30, 2008 5:57 AM
    Moderator

All replies

  • Hi,

     

    For your concern, you can try to refer to the following sample codes:

    Code Snippet

    protected void btnTest_Click(object sender, EventArgs e)
        {
            bool bChanged = HasChanged();
            if (bChanged)
            {
               
    Response.Write("<script language=javascript>confirm(\"Are you sure to change?\");</script>");
            }
        }

     

        //Determine if you change something in a TextBox

        private bool HasChanged()
        {
            return true;
        }

     

    Hope this helps,

    Wednesday, January 30, 2008 5:57 AM
    Moderator
  • Thank you very much for your reply. I am still stuck on this. I use VS 2005.  Response.Write("<script language=javascript>confirm(\"Are you sure to change?\");</script>");

     

    It did not work. I put it in my button_click(), anything I did wrong? Thank you!

    Tuesday, February 05, 2008 5:38 PM
  • Instead of calling the confiirm from onclick you can call another javascript function which checks to see if the text has changed. Maybe you can add a onchange to that textbox that sets a dirty bit when its changed and the new method checks that dirty bit.


    Wednesday, February 06, 2008 5:15 PM