从javascript调用函数或方法后面的代码 [英] call code behind functions or methods from javascript
问题描述
如何从javascript调用asp.net的函数或方法后面的代码?从javascript我需要重新加载我的datagrid通过调用我的代码后面写的函数fillgrid()。
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.
推荐答案
你需要这样的东西,那个技巧是
例如:
1.添加这个HTML代码
Hi,
you need something like this, that trick is
For example:
1. Add this html code
<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。添加此javascript代码
2 . Add this javascript code
<script type="text/javascript">
<pre lang="xml">function doPostBack() {
document.getElementById('<%=btnpostback.ClientID %>').click();
}
< / script>
3.最后将此代码添加到您的代码后面的文件
</script>
3. Finally add this code into your code behind file
protected void btnpostback_Click(object sender, EventArgs e)
{
lbltxtchange.Text=txtinput.Text;
}
问候和谢谢
sarva
regards and thanks
sarva
您无法在客户端脚本中调用服务器端功能,可以使用两种解决方案
1.通过
You can not call a server side function in client side scripts, Two solution is available for you
1. Reload whole of page by
window.location.reload()
或通过ASP.NET控件回发页面。
2.使用Ajax技术
我建议在ASP.NET中使用内置的Ajax。
把你的从Ajax扩展控件 DataGrid
进入 UpdatePanel
然后你可以异步更新它。
for更多信息,请使用关键字ASP.NET Ajax controls
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"
这篇关于从javascript调用函数或方法后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!