Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to call code behind functions or methods of asp.net from javascript? From javascript i need reload my datagrid by calling this function "fillgrid()" which i have written in my code behind.
Posted

Hi,
you need something like this, that trick is

For example:

1. Add this html code

XML
<asp:TextBox ID="txtinput" runat="server" onblur="doPostBack();"></asp:TextBox>
<asp:UpdatePanel ID="niceTrick" runat="server">
<ContentTemplate>
<asp:Button ID="btnpostback" runat="server" Text="btnpostback" onclick="btnpostback_Click" style="display:none" />
<asp:label id="lbltxtchange" runat="server" xmlns:asp="#unknown"></asp:label>
</ContentTemplate>
</asp:UpdatePanel>


2 . Add this javascript code


C#
 <script type="text/javascript">
<pre lang="xml">function doPostBack() {
    document.getElementById('<%=btnpostback.ClientID  %>').click();
}

</script>

3. Finally add this code into your code behind file

C#
protected void btnpostback_Click(object sender, EventArgs e)
{
  lbltxtchange.Text=txtinput.Text;
}


regards and thanks
sarva
 
Share this answer
 
You can not call a server side function in client side scripts, Two solution is available for you
1. Reload whole of page by
JavaScript
window.location.reload()
or post back the page by a ASP.NET control.

2. Use Ajax technology
My recommend is using the built-in Ajax in ASP.NET.
Put your DataGrid into a UpdatePanel from Ajax extension controls then you can update it asynchronously.
for more info please search your favorite search engine with keyword "ASP.NET Ajax controls"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900