Hashir Sarwar
Hashir Sarwar

Reputation: 1285

How to use __doPostBack() function in 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.

Upvotes: 1

Views: 662

Answers (0)

Related Questions