ScriptManager.RegisterStartupScript()方法

如果页面中不用Ajax

cs中运行某段js代码方式可以是:
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>window.open('default2.aspx')</script>");


如果页面中使用了Ajax

则上述代码即使执行也无效果。对这种情况我们通常采用:
ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "success", "alert('更新成功')", true);

 

其中第一个参数为要注册脚本的控件ID。

第二个参数为注册脚本控件类型。

第三个脚本函数的名字,随便起。
第四个是脚本内容。
第五个是标明是否再添加脚本标签,如果第四个参数里包含了<script></script>标签,此处则为false,否则为true。

 

例子

<center>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <table style="width: 80%;" cellspacing="10" cellpadding="10" class="table">
                    <tr>
                        <td>
                            工号
                        </td>
                        <td>
                            <asp:TextBox ID="txtNum" runat="server" CssClass="input"

                                 ReadOnly="true"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            姓名
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server"

                                 CssClass="input"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            性别
                        </td>
                        <td>
                            <asp:TextBox ID="txtGender" runat="server"

                                   CssClass="input"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            密码
                        </td>
                        <td>
                            <asp:TextBox ID="txtPwd" runat="server"

                                 CssClass="input"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            手机号
                        </td>
                        <td>
                            <asp:TextBox ID="txtCellphone" runat="server"

                                   CssClass="input"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            出生年月
                        </td>
                        <td>
                            <asp:TextBox ID="txtBirthday" runat="server"

                                  CssClass="input"></asp:TextBox>
                            <cc1:CalendarExtender ID="CalendarExtender1" runat="server"

                                 TargetControlID="txtBirthday"
                                 Format="yyyy-MM-dd">
                            </cc1:CalendarExtender>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
    </center>
    <ul>
        <li><span>
            <asp:Button ID="btnSubmit" runat="server" Text="提交" CssClass="btn"

                 OnClick="btnSubmit_Click" /></span>
            <span>
                <asp:Button ID="btnClear" runat="server" Text="重置" CssClass="btn"

                  OnClick="btnClear_Click" /></span>
            <span id="wait"></span>
        </li>
    </ul>

 

 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                newTeacherBLL.ModifyNameByNum(this.txtNum.Text, this.txtName.Text);
                newTeacherBLL.ModifyGenderByNum(this.txtNum.Text, this.txtGender.Text);
                newTeacherBLL.ModifyPwdByNum(this.txtNum.Text, this.txtPwd.Text);
                newTeacherBLL.ModifyCellPhoneByNum(this.txtNum.Text, this.txtCellphone.Text);
                newTeacherBLL.ModifyBirthDayByNum(this.txtNum.Text, Convert.ToDateTime

                             (this.txtBirthday.Text));
                ScriptManager.RegisterStartupScript(UpdatePanel1, typeof

                             (UpdatePanel), "success", "alert('更新成功')", true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, typeof

                           (UpdatePanel), "wrong", "alert('更新失败,请重新操作')", true);
            }
          
        }

你可能感兴趣的:(server,脚本,asp,button,textbox,triggers)