Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i call a server side button click function from client side?

I have a server side function

protected void SearchButton_Click(object sender, EventArgs e)       
{

}

I need to call the same from a client side script

function calSearch()
{
    // here I need to call the client side script
}
like image 252
Kailas Avatar asked Mar 18 '14 06:03

Kailas


People also ask

How do I call a server-side function from a JavaScript function?

The EnablePageMethods attribute must be added on the ScriptManager tag as true. Drag and drop a Button control onto the form to show the message when you click on the Button. The "OnClientClick" event will be associated with the JavaScript function. The code above defines how to call a server-side function named "Name".

How to see the server-side code message on the button control?

If there are no errors then "Success" will show a pop-up window with our server-side message text. Else, the pop-up will show an exception message. Now press F5 to run the application. Now click on the Button control to see the server-side code message.

How to make a server side method callable from remote clients?

The attach the " [WebMethod]" attribute to a server-side method to make the method callable from remote Web clients. To learn More A static method is callable on a class even when no instance of the class has been created. If any instances of the class are created then they cannot be used to access the static member.

How do I add a server-side message to a form?

Drag and drop a Button control onto the form to show the message when you click on the Button. The "OnClientClick" event will be associated with the JavaScript function. The code above defines how to call a server-side function named "Name". If there are no errors then "Success" will show a pop-up window with our server-side message text.


2 Answers

You cant do this, your option is to simulate Click of a button using javascript.

<asp:Button ID="savebtn" runat="server" OnClick="savebtn_Click" style="display:none" />

HTML markup will look like this:

<button id="btnsave" onclick="fncsave()">Save</button>

now simulate a Click using JS

<script type="text/javascript">
     function fncsave()
     {
        document.getElementById('<%= savebtn.ClientID %>').click();
     }
</script>

Other solution would be to use ajax.

like image 140
highwingers Avatar answered Oct 16 '22 20:10

highwingers


you can use __doPostBack method

function functionName()
{

__doPostBack('ButtonId','OnClick');

}

or you can try this....

<button id="btnsave" onclick="callfunction()">Save</button>// to all javascript
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" style="display:none" />


<script type="text/javascript">
     function callfunction()
     {  
         /*do your javascript coding here*/
        document.getElementById('<%= btnSave.ClientID %>').click();
       // we are firing onClick event for  asp button btnsave form javascript.
     }
</script>

otherwise you can create WebMethod in code behind and make AJAX call but then you wont be able to access asp control in WebMethod.

   $.ajax({
            type: "POST",
            url: "AspDotNetPage{}'{"+myData+"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
               alert(msg);                
            }
          });
like image 42
juhi Avatar answered Oct 16 '22 20:10

juhi