• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

asp.net LinkButton - DO an ajax request and then submit the form

Dear experts,

I have a form and to submit the form I am using a linkButton. My linkbutton also does some client side validation before submitting the form. A requiremtn has now come in that I do some server side validation before submitting the form. This means the linkbutton first needs to send an ajax request (without doing a page refresh) and based on the result give the user a modal popup. On the modal popup if the user clicks continue, i need to submit the form otherwise cancel submitting the form.

I am really stuck and have no idea if/how thsi can be achieved. Please help!
0
vbhargav80
Asked:
vbhargav80
  • 2
2 Solutions
 
amar31282Commented:
you need to use javascript as
    function TestFunction(data)
    {
        var flag = true;
        if( data == "") 
        {
            flag = confirm("You have not entered any name. This will result in all the records.\nDo you want to Continue...");
        }
        return flag;
    }
 
In link button use
OnClientClick="return TestFunction(document.getElementById('Simple_Search_Faculty_Name_Input').value);"

Open in new window

0
 
mahadevan_vCommented:
I will give you an overview of how it can be done ..

1. write a javascript function and attach it to the link button ,
2. In that function send an ajax request to the page where you have the server side validation.
    Return a value from the server page
3. On the function which process the response, compare the value from server , then open a modal dialog using showmodal dialog function
vReturnValue = window.showModalDialog()
4. now check the return value from the modal for submitting or cancel page progress


0
 
vbhargav80Author Commented:
My LinkButotn already has some javascript attached to it which is doing the client side validation. How can I attach 2 different pieced of javascript to it, 1 that does client side and 1 that does server side?
0
 
mahadevan_vCommented:
no need to add another function

after the client side validation create an ajax request to the server page

function ajaxFunction()
{
// DO YOUR CLIENT VALIDATIIOIN//if all your client validations are passed call the server page with validations using the below code..

var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.myForm.time.value=xmlHttp.responseText;
//This statement get value from the server page and sets it to control add a client validation for checking the return value
      }
    }
  xmlHttp.open("POST","time.asp",true);
  xmlHttp.send(null);
  }



Check this link for examples

http://www.w3schools.com/Ajax/ajax_example.asp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now