1

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

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

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

4

4 回答 4

3

试试下面的例子

第一个例子

在 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> 
于 2009-08-05T14:59:12.403 回答
0

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

OnClientClick仅用于调用 javascript 代码等客户端脚本。如果您尝试在代码中调用方法,则应使用OnClick事件。

于 2009-04-01T10:16:03.520 回答
0

将您的代码更改为:

<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,没有",因为它们在<%# %>标签内使用

于 2009-04-01T11:53:17.297 回答
0

你可以试试这个。

一世。在后面的代码中

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

ii. 第二种方法是使用内联 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 在按钮之前加载。

于 2009-04-01T13:51:07.543 回答