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












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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.







      asp.net onclick postback dopostback






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 18:58







      Hashir Sarwar

















      asked Nov 28 '18 at 16:16









      Hashir SarwarHashir Sarwar

      14011




      14011
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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%>','')





          share|improve this answer


























          • after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

            – Hashir Sarwar
            Nov 28 '18 at 16:36











          • It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

            – Sean T
            Nov 28 '18 at 16:39













          • I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

            – Sean T
            Nov 28 '18 at 16:40













          • still not working for me

            – Hashir Sarwar
            Nov 28 '18 at 18:57











          • @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

            – Sean T
            Nov 29 '18 at 9:39



















          0














          Looks like you are missing the OnClick in asp:Button.



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



          There is a in-built method in ASP.NET to generate the __doPostBack() method.



          <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>; 


          If still not working after the changes, I would suggest you to check the network traffic using Network tab of browser Debug tools(or Fiddler) when you click on the button.



          Also set a break point inside the reg() method to see how the control goes.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523797%2fdopostback-function-isnt-working-asp-net%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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%>','')





            share|improve this answer


























            • after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

              – Hashir Sarwar
              Nov 28 '18 at 16:36











            • It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

              – Sean T
              Nov 28 '18 at 16:39













            • I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

              – Sean T
              Nov 28 '18 at 16:40













            • still not working for me

              – Hashir Sarwar
              Nov 28 '18 at 18:57











            • @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

              – Sean T
              Nov 29 '18 at 9:39
















            1














            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%>','')





            share|improve this answer


























            • after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

              – Hashir Sarwar
              Nov 28 '18 at 16:36











            • It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

              – Sean T
              Nov 28 '18 at 16:39













            • I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

              – Sean T
              Nov 28 '18 at 16:40













            • still not working for me

              – Hashir Sarwar
              Nov 28 '18 at 18:57











            • @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

              – Sean T
              Nov 29 '18 at 9:39














            1












            1








            1







            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%>','')





            share|improve this answer















            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%>','')






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 16:44

























            answered Nov 28 '18 at 16:20









            Sean TSean T

            1,356715




            1,356715













            • after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

              – Hashir Sarwar
              Nov 28 '18 at 16:36











            • It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

              – Sean T
              Nov 28 '18 at 16:39













            • I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

              – Sean T
              Nov 28 '18 at 16:40













            • still not working for me

              – Hashir Sarwar
              Nov 28 '18 at 18:57











            • @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

              – Sean T
              Nov 29 '18 at 9:39



















            • after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

              – Hashir Sarwar
              Nov 28 '18 at 16:36











            • It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

              – Sean T
              Nov 28 '18 at 16:39













            • I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

              – Sean T
              Nov 28 '18 at 16:40













            • still not working for me

              – Hashir Sarwar
              Nov 28 '18 at 18:57











            • @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

              – Sean T
              Nov 29 '18 at 9:39

















            after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

            – Hashir Sarwar
            Nov 28 '18 at 16:36





            after returning true, how it will get into Btn_Click function? i didn't specify it anywhere.

            – Hashir Sarwar
            Nov 28 '18 at 16:36













            It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

            – Sean T
            Nov 28 '18 at 16:39







            It's been a while since I did webforms so as I say that may be incorrect. you may have to specify onclick attribute in addition to onclientclick. Anyhow the first part of the answer is correct

            – Sean T
            Nov 28 '18 at 16:39















            I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

            – Sean T
            Nov 28 '18 at 16:40







            I just recall that if you return true on client click it will go to the server click event, and since it's a submit button this will be posted

            – Sean T
            Nov 28 '18 at 16:40















            still not working for me

            – Hashir Sarwar
            Nov 28 '18 at 18:57





            still not working for me

            – Hashir Sarwar
            Nov 28 '18 at 18:57













            @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

            – Sean T
            Nov 29 '18 at 9:39





            @HashirSarwar just put a click attribute on the button then that calls the server side function and return false under your write all fields line

            – Sean T
            Nov 29 '18 at 9:39













            0














            Looks like you are missing the OnClick in asp:Button.



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



            There is a in-built method in ASP.NET to generate the __doPostBack() method.



            <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>; 


            If still not working after the changes, I would suggest you to check the network traffic using Network tab of browser Debug tools(or Fiddler) when you click on the button.



            Also set a break point inside the reg() method to see how the control goes.






            share|improve this answer






























              0














              Looks like you are missing the OnClick in asp:Button.



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



              There is a in-built method in ASP.NET to generate the __doPostBack() method.



              <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>; 


              If still not working after the changes, I would suggest you to check the network traffic using Network tab of browser Debug tools(or Fiddler) when you click on the button.



              Also set a break point inside the reg() method to see how the control goes.






              share|improve this answer




























                0












                0








                0







                Looks like you are missing the OnClick in asp:Button.



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



                There is a in-built method in ASP.NET to generate the __doPostBack() method.



                <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>; 


                If still not working after the changes, I would suggest you to check the network traffic using Network tab of browser Debug tools(or Fiddler) when you click on the button.



                Also set a break point inside the reg() method to see how the control goes.






                share|improve this answer















                Looks like you are missing the OnClick in asp:Button.



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



                There is a in-built method in ASP.NET to generate the __doPostBack() method.



                <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>; 


                If still not working after the changes, I would suggest you to check the network traffic using Network tab of browser Debug tools(or Fiddler) when you click on the button.



                Also set a break point inside the reg() method to see how the control goes.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '18 at 20:58

























                answered Nov 28 '18 at 20:51









                Charmis VargheseCharmis Varghese

                806




                806






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523797%2fdopostback-function-isnt-working-asp-net%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks