Server tag in OnClientClick

前端 未结 4 1624
孤独总比滥情好
孤独总比滥情好 2021-01-05 02:03

The following gives me an error of \"The server tag is not well formed\"



        
相关标签:
4条回答
  • 2021-01-05 02:48

    I found this answer over at www.asp.net

    OnClientClick='<%# Eval("ProductName", "return confirm(""Delete the Product {0}?"")" ) %>'
    

    This puts everything in the markup so anyone doing maintenance later doesn't have dig around to find all of the pieces.

    0 讨论(0)
  • 2021-01-05 02:52

    Add the code dynamically in the ItemDataBound event for the ListView control.

    In your page_Load event add the following

    lst.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lst_ItemDataBound);
    

    Then in your ItemDataBound event handler add

    Control DeleteButton = e.Item.FindControl("DeleteButton");
    DeleteButton.OnClientClick = string.Format( "return confirm('Are you sure you want to delete '{0}'?", Username);
    

    This solution should work whether you use OnClientClick or Sachin Gaur's solution.

    0 讨论(0)
  • 2021-01-05 02:57

    You can add the onclick event at run time, like this:

    DeleteButton.Attributes.Add("onclick", "'return confirm('Are you sure you want to delete '" + Username);
    


    0 讨论(0)
  • 2021-01-05 03:03

    The problem is the binding nugget and the use of single and double quotes.

    <asp:LinkButton D="DeleteButton" runat="server" CommandName="Delete" OnClientClick='<%# CreateConfirmation(Eval("Username")) %>'>Delete</asp:LinkButton>
    

    Then on the code-behind add the function...

    Public Function CreateConfirmation(ByVal Username As String) As String
        Return String.Format("return confirm('Are you sure you want to delete {0}?');", Username)
    End Function
    

    When the binding nugget is used as the value for an attribute, you'll note you have to use single quotes. Your script also needed quotes for the embedded string parameter to the confirm function. You basically ran out of quotes.

    0 讨论(0)
提交回复
热议问题