• 在asp.net ajax中使用javascript


    aspx
        <form id="form1" runat="server">

            
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

        
    </form>

    cs
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        
    protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScriptManager cs 
    = this.ClientScript;
            cs.RegisterArrayDeclaration(
    "Hello""1, 2, 3");
            cs.RegisterClientScriptBlock(
    this.GetType(), "HelloWorld""function helloWorld(){alert(1);}"true);
            cs.RegisterClientScriptInclude(
    "HelloWorld""HelloWorld.js");
            
    // cs.RegisterClientScriptResource(
            cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello""World");
            cs.RegisterHiddenField(
    "hello""world");
            cs.RegisterOnSubmitStatement(
    this.GetType(), "HelloWorld""return window.confirm('Do you really want to submit the form?')");
            cs.RegisterStartupScript(
    this.GetType(), "HelloWorld""<script>alert('The page has loaded!')</script>");
        }

    点击Button1后生成的html
        <form name="form1" method="post" action="ClientScriptManager.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
    <div>
    <input type="hidden" name="hello" id="hello" value="world" />
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ2OTkzNDMyMWRkXQusG/GEJI5thnlOYBE9TxuzDXA=" />
    </div>


    <script type="text/javascript">
    //<![CDATA[
    function helloWorld(){alert(1);}//]]>
    </script>

    <script src="HelloWorld.js" type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function WebForm_OnSubmit() {
    return window.confirm('Do you really want to submit the form?');
    return true;
    }
    //]]>
    </script>


            
    <input type="submit" name="Button1" value="Button" id="Button1" />

        
    <script type="text/javascript">
    //<![CDATA[
    var Hello =  new Array(123);
    //]]>
    </script>

    <script type="text/javascript">
    //<![CDATA[
    var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
    Button1.Hello 
    = "World";
    //]]>
    </script>

    <div>

        
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLg9frACwKM54rGBnQtBc2fVh5CkTBe8MjX+hmuyng2" />
    </div>
    <script>alert('The page has loaded!')</script></form>

    注意使用RegisterStartupScript方法生成的javascript是添加到</form>标签前面的
    而是用RegisterClientScriptBlock方法成的javascript是添加到<form>后面的,生成的位置不一样
  • 相关阅读:
    python爬取图片
    TensorFlow学习系列(四):minist实例--卷积神经网络
    TensorFlow学习系列(四):minist实例--用简单的神经网络训练和测试
    TensorFlow学习系列(三):实例数据下载和读取
    TensorFlow学习系列(二):入门起步
    TensorFlow学习系列(一):安装调试
    Anaconda安装
    screen的安装和使用
    YII 使用mysql语句查询
    YII 返回值为JSON格式
  • 原文地址:https://www.cnblogs.com/timy/p/1167894.html
Copyright © 2020-2023  润新知