【问题标题】:C# OnclientClick doens't render asp tags?C# OnclientClick 不呈现 asp 标签?
【发布时间】:2009-04-01 09:27:35
【问题描述】:

我有一个看起来像这样的 ASP 按钮:

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

问题是 de hideOverlay 部分中的 asp 标签。我无法正常工作。为什么不工作?我该如何解决?

【问题讨论】:

    标签: c# onclientclick


    【解决方案1】:

    试试下面的例子

    第一个例子

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

    在代码隐藏中

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

    它将为按钮生成以下源代码

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

    第二个例子

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

    在代码隐藏中

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

    它将为按钮生成以下源代码

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

    第三个例子

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

    它将为脚本部分生成以下源代码

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

    【讨论】:

      【解决方案2】:

      尝试将内联代码中的“=”替换为“#”。例如 => ,以便在编译时实例化 ClientId

      OnClientClick 仅用于调用 javascript 代码等客户端脚本。如果你试图在代码后面调用一个方法,你应该使用 OnClick 事件。

      【讨论】:

        【解决方案3】:

        将您的代码更改为:

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

        如果你使用string.Format(),甚至更好

        OnClientClick=<%# string.Format("hideOverlay('{0}', '{1}');", pnlOverlay.ClientID,pnlAddComment.ClientID) %>
        

        不要忘记对链接进行数据绑定,是的 onclientclick 没有 " ,因为它们在 &lt;%# %&gt; 标记中使用

        【讨论】:

        • 我试过你的代码,但它不起作用。当我查看源代码时,我看到: onclick 属性没有了,怎么来?.数据绑定链接是什么意思?
        • 在您的代码隐藏中,您必须调用“DataBind();”或“btnReset.DataBind()”
        【解决方案4】:

        你可以试试这个。

        我。在后面的代码中

        btnReset.OnClientClick = "hideOverlay'"+pnlOverlay.ClientID+"','"+pnlAddComment.ClientID+"')";
        

        二。第二种方法是使用内联 javascript。

        <asp:Button ID="btnReset" runat="server" OnClientClick = "newhideOverlay()" CssClass
        ="btnCancel PopUpButton"/>
        
        <script type="text/javascript">
        
        function newhideOverlay() 
        
        {    
              hideOverlay('<%=pnlOverlay.ClientID %>', '<%=pnlAddComment.ClientID  %>');
        
        } 
        
        </script>
        

        还要确保 pnlOverlay 和 pnlAddComment 在按钮之前加载。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-12-12
          • 1970-01-01
          • 1970-01-01
          • 2014-10-14
          • 2021-07-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多