2009-04-01 6 views
1

J'ai un bouton ASP qui lookts comme ceci:C# OnclientClick ne rend pas les tags ASP?

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

Le problème sont les balises asp de hideOverlay part.I ne reçoivent pas de travail. Pourquoi ne travaille pas? Et comment puis-je le réparer?

Répondre

3

Essayez ci-dessous des exemples

Premier exemple

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

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

    } 
} 

Il va générer la source ci-dessous pour le bouton

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

Deuxième exemple

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

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

Il va générer la source ci-dessous pour le bouton

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

Troisième exemple

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

Il va générer la source follwing pour la partie du script

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

     //Do your stuff 
    } 
</script> 
0

Essayez de remplacer "=" par "#" dans votre code en ligne. par exemple. <% = pnlOverlay.ClientID%> =><% # pnlOverlay.ClientID%>, de sorte que la ClientId est instancié dans le temps de la compilation.

OnClientClick est uniquement utilisé pour appeler un script côté client tel que du code javascript. Si vous essayez d'appeler une méthode dans le code, vous devez utiliser l'événement OnClick.

0

Changer votre code:

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

ou encore plus agréable si vous utilisez string.Format()

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

Et ne pas oublier de DataBind le lien, et oui l'onclientclick n'a pas ", puisque ceux-ci sont utilisés à l'intérieur des étiquettes <%# %>

+0

J'ai essayé votre code, mais il ne fonctionne pas. Quand je regarde la source je vois: L'attribut onclick est parti , comment venir?. Et que voulez-vous dire avec le lien databind? – Martijn

+0

Dans votre codebehind vous devez appeler "DataBind();" ou "btnReset.DataBind()" – Gidon

0

Vous pouvez essayer ceci.

i. Dans le code suivant

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

ii. La deuxième approche consiste à utiliser un javascript en ligne.

<asp:Button ID="btnReset" runat="server" OnClientClick = "newhideOverlay()" CssClass 
="btnCancel PopUpButton"/> 

<script type="text/javascript"> 

function newhideOverlay() 

{  
     hideOverlay('<%=pnlOverlay.ClientID %>', '<%=pnlAddComment.ClientID %>'); 

} 

</script> 

Assurez-vous également que pnlOverlay et pnlAddComment se chargent avant le bouton.