从后面的代码运行javascript

时间:2013-01-08 11:49:55

标签: javascript asp.net ajax code-behind

我知道有很多这样的问题。我尝试了很多,但仍然无法使它工作。

我有:RadAjaxManagerRadAjaxPanelRadScriptBlock

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" 
         DefaultLoadingPanelID="RadAjaxLoadingPanel1">
         <ClientEvents OnRequestStart="onRequestStart" />
             <AjaxSettings>
                  <telerik:AjaxSetting AjaxControlID="AjaxPanel">
                      <UpdatedControls>
                          <telerik:AjaxUpdatedControl ControlID="AjaxPanel"/>
                       </UpdatedControls>
                   </telerik:AjaxSetting>
              </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxPanel runat="server" ID="AjaxPanel" Wrap="True" CssClass="PanelAjax">
         <telerik:RadComboBox runat="server" ID="cbLanguage" Width="115px" 
             AutoPostBack="True"Sort="Ascending" EmptyMessage="Choose language" 
             DataValueField="id" DataTextField="languageName"
             OnSelectedIndexChanged="cbLanguage_SelectedIndexChanged">
          </telerik:RadComboBox>
    </telerik:RadAjaxPanel>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
      <script>
          function MyFunction(){
                  alert('asdasd');
          }
      </script>
    </telerik:RadScriptBlock>
</asp:Content>

Master Page我有:

<telerik:RadScriptManager ID="ScriptManager1" runat="server">
</telerik:RadScriptManager>

我想在combo box selected index change上运行MyFunction()。

我已经尝试过了:

protected void cbLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "myKey", "MyFunction();");
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "testOne", "MyFunction();", true);
}

我也试着直接发出警报:

ScriptManager.RegisterStartupScript(Page, GetType(), "alert", "alert('asd');", false);

RadAjaxManager1.ResponseScripts.Add(@"radalert('Welcome to RadWindow <b>Prometheus</b>!', 330, 210);");

string treeScript = "<script language='javascript'>alert('asd');</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", treeScript);

RadAjaxManager1.ResponseScripts.Add(@"alert('Welcome');");

有人可以解释一下我做错了什么吗?

没有错误。 只是javascript代码没有执行。我的屏幕上没有alert()窗口。

1 个答案:

答案 0 :(得分:2)

你的javascript中应该出现语法错误!您的声明中有拼写错误。

您写道:

  <script>
      fuction MyFunction(){
              alert('asdasd');
      }
  </script>

但必须是:

  <script>
      function MyFunction(){
              alert('asdasd');
      }
  </script>