Asp.net 中通过javascript调用后台函数

Asp.NET中,当服务器控件提交到服务器的时候,会调用__doPostBack(eventTarget, eventArgument)这个函数,当在页面中添加一个LinkButton的时候,在页面生成的html代码中可以看到__doPostBack函数的定义,第一个参数为服务器控件id,第二个参数是该控件所带的参数。 

<script type="text/javascript">
<!--

var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->

</script>

 

aspx页面主要代码如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页title>
    <script type="text/javascript">
    function excute()
    {
        __doPostBack('LinkButton1','');
    }
    script>
head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
        <input id="Button1" type="button" value="button" onclick="JavaScript:excute();"/>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">asp:LinkButton>
        
            div>
    form>
body>
html>

后台cs文件主要代码:

namespace Javascript调用cs
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "调用成功";
        }
    }
}

你可能感兴趣的:(JavaScript,server,asp.net,asp,button,textbox)