最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i call a server side button click function from client side? - Stack Overflow

programmeradmin1浏览0评论

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
}

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
}
Share Improve this question edited Jun 23, 2014 at 15:07 BenMorel 36.5k51 gold badges205 silver badges335 bronze badges asked Mar 18, 2014 at 6:14 KailasKailas 4491 gold badge5 silver badges14 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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.

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);                
            }
          });

Assuming Id of your button is SearchButton , you can do this

function calSearch(){

__doPostBack('SearchButton','');

}

Note: There are two underscores before doPostBack() function

A better way to do this here

发布评论

评论列表(0)

  1. 暂无评论