0

I write my project in Asp.net using JavaScript.My code is below:

<script type="text/javascript">
     . . .
     var btnSaveInsert = document.getElementById("btnSaveInsert");
     . . .
     btnSaveInsert.onclick = function (e) {
          e.preventDefault();
          var s = <%=Save() %>;
          modal2.style.display = "none";
          modal3.style.display = "block";
     }

This is my script tag.There are other functions too in script before this. As you look, I call my Save method in button's click.

<input type="button" class="button-style" style="width:50px; height:30px; margin-left:50%;" id="btnInsertOK" value="OK"/>  

And my Save() method in code behind:

protected string Save()
        {
            DataTable dt = new DataTable();
            string json;
            string active = Request.Form["txtActive"];
            string code = Request.Form["txtCode"];
            string name = Request.Form["txtName"];
            string warehouse = Request.Form["txtWarehouse"];
            string url = "http://InsertData?active=" + active + "&code=" + code + "&name=" + name + "&warehouse=" + warehouse + "";

            using (WebClient client = new WebClient())
            {
                json = client.DownloadString(url);
            }
            string[] json1 = json.Split('>');
            string[] json2 = json1[2].Split('<');
            json = json2[0];

            return json;
        }

When the project start running firstly, Save() runs, and it inserts default string.How can I fix this problem?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
fico
  • 9
  • 2
  • 8
  • 1
    You're not calling `Save()` in your method's button click event. You're populating the JavaScript code (in an attempt to populate `s`, I guess) with the result of `Save()` at the point in time when the page is rendered. – ProgrammingLlama Dec 05 '18 at 07:23
  • 1
    What use is `var s = <%=Save() %>;`? This is where it makes a call to `Save` method from code-behind. – shahkalpesh Dec 05 '18 at 07:24
  • **s** is bool variable. If insert is successfully it returns true. And I want to show message about this according to value of **s**.What do you think?How can I call `Save()`? – fico Dec 05 '18 at 07:30
  • 1
    Similar links https://stackoverflow.com/questions/28725992/call-c-sharp-function-from-javascript-onclick-event-button https://stackoverflow.com/questions/7646162/how-to-fire-a-button-click-event-from-javascript-in-asp-net – sjd Dec 05 '18 at 07:31
  • I would suggest reading up on asp.net server controls. Create the save button with `Save` and make sure you have an eventhandler that will be invoked when you click on save, in which you call `Save` method that you have written. – shahkalpesh Dec 05 '18 at 09:43
  • The `<%=Save() %>` runs on the server, *then* the text is sent to the browser where it is treated as javascript. The `<% %>` is gone by then. You probably want to investigate "ajax" – Hans Kesting Dec 05 '18 at 10:19

0 Answers0