Search code examples
c#onclientclick

C# OnclientClick doens't render asp tags?


I have an ASP button that lookts like this:

<asp:Button 
  ID="btnReset" 
  runat="server" 
  OnClientClick = "hideOverlay('<%=pnlOverlay.ClientID %>', '<%=pnlAddComment.ClientID  %>');"
  CssClass ="btnCancel PopUpButton"
/>

The problem are the asp tags in de hideOverlay part.I don't get it working. Why isn't working? And how do i fix it?


Solution

  • Try below examples

    First Example

    In aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" />
            <asp:Panel ID="pnlOverlay" runat="server">
            </asp:Panel>
            <asp:Panel ID="pnlAddComment" runat="server">
            </asp:Panel>        
        </div>
        </form>
    </body>
    </html>
    

    In Codebehind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           btnReset.Attributes.Add("onclick", string.Format("hideOverlay('{0}','{1}')", pnlOverlay.ClientID, pnlAddComment.ClientID));
    
        }
    }
    

    It will generate the below source for the button

    <input type="submit" name="btnReset" value="" onclick="hideOverlay('pnlOverlay','pnlAddComment');" id="btnReset" class="btnCancel PopUpButton" />
    

    Second Example

    In Aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" OnClientClick=<%# "hideOverlay('" + pnlOverlay.ClientID + "', '" + pnlAddComment.ClientID +"');" %> />
            <asp:Panel ID="pnlOverlay" runat="server">
            </asp:Panel>
            <asp:Panel ID="pnlAddComment" runat="server">
            </asp:Panel>        
        </div>
        </form>
    </body>
    </html>
    

    In CodeBehind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnReset.DataBind(); 
        }
    }
    

    It will generate the below source for the button

    <input type="submit" name="btnReset" value="" onclick="hideOverlay('pnlOverlay', 'pnlAddComment');" id="btnReset" class="btnCancel PopUpButton" />
    

    Third Example

    In aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnReset" runat="server" CssClass="btnCancel PopUpButton" OnClientClick="hideOverlay();" />
            <asp:Panel ID="pnlOverlay" runat="server">
            </asp:Panel>
            <asp:Panel ID="pnlAddComment" runat="server">
            </asp:Panel>        
        </div>
        </form>
    </body>
    <script type="text/javascript" >
        function hideOverlay()
        {
            var pnlOverlayID = '<%= pnlOverlay.ClientID %>';
            var pnlAddCommentID = '<%= pnlAddComment.ClientID %>';
    
            //Do your stuff
        }
    </script> 
    </html>
    

    It will generate the follwing source for the script portion

    <script type="text/javascript" >
        function hideOverlay()
        {
            var pnlOverlayID = 'pnlOverlay';
            var pnlAddCommentID = 'pnlAddComment';
    
            //Do your stuff
        }
    </script>