Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__doPostBack() function isn't working (asp.net)

I am trying to trigger button event from the JS code. But doPostBack in JS function reg() is not directing to the c# code. Please tell me what's wrong. Here is my code:

<script>
        function reg() {
            var name = document.getElementById('name').value;
            var id = document.getElementById('cnic').value;
            var age = document.getElementById('age').value;
            var ph = document.getElementById('phone').value;
            var pas = document.getElementById('pass').value;
            if (id == '' || pas == '' || age == '' || ph == '' || pas == '')
                window.alert("Write all fields");
            else {
                __doPostBack('<%= Button1.UniqueID%>','')
            }
        }
    </script>  


 <div >
       <asp:Button id="Button1" OnClientClick="reg()" runat="server" Text="Submit"/>
    </div>

Here is the server side c# function associated with the button:

 protected void Btn_Click(object sender, EventArgs e)
    {
        Button clickedButton = (Button)sender;
        clickedButton.Text = "...button clicked...";
    }

Note: In else block, I want reg() function to redirect to the Btn_Click function.

like image 866
Hashir Sarwar Avatar asked Apr 06 '26 17:04

Hashir Sarwar


1 Answers

It's been a while since I did this, but I think it needs to be the server Id, not client id of the button.

 __doPostBack('<%= button1.UniqueID%>','')
like image 156
Sean T Avatar answered Apr 08 '26 12:04

Sean T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!