从Code后面调用javascript方法

时间:2016-01-14 09:40:01

标签: javascript c# asp.net function

此代码适用于其他页面 但它没有在新页面中工作

要调用的JS函数:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
  <script type="text/javascript">
    function myfun() {
      alert("test");
    }
  </script>
</asp:Content>

Html代码:

<asp:TemplateField>
  <ItemTemplate>
    <asp:ImageButton CssClass="Link" ID="IMGDetail" 
         ClientIDMode="Static" runat="server" 
         ImageUrl="~/Img/Detail.png" Height="19px" 
         Width="19px" OnClick="IMGDetail_Click" ToolTip="Detail" />
  </ItemTemplate>
</asp:TemplateField>

代码背后:

protected void IMGDetail_Click(object sender, ImageClickEventArgs e) {
  LinkButton lbtn = (LinkButton) sender;
  GridViewRow gvr = (GridViewRow) lbtn.NamingContainer;
  Label LabelTicketId = (gvr.FindControl("LBTicketId") as Label);
  Session["TicketId"] = LabelTicketId.Text;
  ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "myfun()", true);
}

1 个答案:

答案 0 :(得分:0)

我认为您需要在aspx页面中添加以下标记。

<asp:ScriptManager ID="ScriptManager" runat="server" />

在Codebehind中

ScriptManager.RegisterStartupScript(this, typeof(string), "myfun", "myfun()", true);