Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this serious:

I have ASP.NET page, This page contents Update panel with ASP.NET control.

I have Java script function to do validation so when I click the button I will use onclientclick to call the java function to do the validation and after this one done should call then event click button function from code behind.

I tried vew methods but they did not work for me.

here is sample of my code that after I click the button onclientclick will call the java script function for validation and if the validation is OK should call onclick event.

.................... java script function ........................

    <script type="text/javascript" >

function add(){
 if (tag == trye) {


                         document.getElementById('<%=btnInfor.ClientID%>').click();
                         alert("DataAdded")



                    }
                    else {

                          alert("Requiered Field Missing.")
                        return false;

                     }
}

</script> 
.....................
ASP.NET button
...................
<asp:Button ID="btnInfor" runat="server" Text="Add Information" Style="position: absolute;
                top: 1659px; left: 433px;" 
                onclientclick="JavaScript: return myAdd()" />
....................
code behind in C#
......................
 protected void btnInfor_Click(object sender, EventArgs e)
        {    
                \\mycode

}
share|improve this question

2 Answers

up vote 0 down vote accepted

The way you have it setup is that when you call document.getElementById( buttonname).click() it keeps calling the javascript function, and never actually gets to the server side method you want called.

First assign the server side click event handler:

<asp:Button ID="btnInfor" runat="server" Text="Add Information" Style="position: absolute;
            top: 1659px; left: 433px;" OnClick="btnInfor_Click"
            onclientclick="JavaScript: return myAdd()" />

Then in your javascript function, return true in the if branch:

function add(){
    if (tag == trye) {
        alert("DataAdded");
        return true; 
    }
    else {
        alert("Requiered Field Missing.")
         return false;
    }
}
share|improve this answer

I see 2 things that would prevent the C# code from getting called:

  1. The Javascript function should have return true; after the line alert("DataAdded")
  2. You need to "wire up" your c# event handler by adding an attribute to the button, for example...OnClick="btnInfor_Click"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.