0

Here is my code. I am calling a JavaScript function in the code after insert query fires. But I get the error "return statement outside of function".

ClientScript.RegisterStartupScript(GetType(), "id", "return showPatientInfo('" + txtPatientID.Text + "');",true);

function showPatientInfo(pid) {
    //alert(pid);
    RedirectToPage("Create_Order.aspx", "pid=" + pid);
    return false;
}

1 Answer 1

1

I really wanted to rewrite your code as more idiomatic JavaScript. In the process, I found that formBypassQueryString variable was never getting assigned, which would probably cause problems. Here is my version:

function showPatientInfo(pid) {
  redirectToPage("Create_Order.aspx", "pid=" + pid);
  return false;
}

function redirectToPage(pageName, queryString) {
  if (pageName === "" || queryString === "") return;

  var keyValues = queryString.split("&"),
      form = createQueryStringForm(pageName);

  for (var i = 0; i < keyValues.length; i++) {
    var keyValue = keyValues[i].split("="),
        field = createQueryStringField(keyValue[0], keyValue[1]);

    form.appendChild(field);
  }

  form.submit();
}

function createQueryStringField(name, value){
  var field = document.createElement('input');

  field.setAttribute('type', 'hidden')
  field.setAttribute('name', name)
  field.setAttribute('value', value)

  return field;
}

function createQueryStringForm(pageName){
  var form = document.createElement('form');

  form.setAttribute('id', 'form-bypass-query-string');
  form.setAttribute('method', 'POST');
  form.setAttribute('action', pageName);

  return form;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi MATT, i have applied ur code but getting same error "return statement outside of function"
@harsh: Other then that variable not being defined (which would cause an error, but not what you were getting), there is nothing technically wrong with your code. My version is just closer to the way a javascript guy would do the same thing :)
:when i remove return statement from code behind..ClientScript.RegisterStartupScript(GetType(), "id", "return showPatientInfo('" + txtPatientID.Text + "');",true); ....code executes fine but getting error "object doesn't support this property or method"..and when i add return statement then give me error "return statement outside of function"..i think should my syntax is correct when i call javascript function from codebehind

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.