简体   繁体   中英

Calling javascript Method from Code behind

This code worked in another page But it's not working in the new page

JS Function to be called:

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

Html code:

<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>

Code behind:

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);
}

I think you need to add following tag in your aspx page.

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

In Codebehind

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM