ASP.NET Usercontrol Property value does not run &l

2019-09-10 10:51发布

问题:

Well i have a usercontrol with a property "ClientScript" and in the aspx file where i use the usercontrol i set the value to =document.getElementsByName('<%=ReportViewer1.ClientId %>$ctl01$ctl07$ctl00$ctl00$ctl00')[0].click(); return false;

the problem here is that the is litterly passed to the property and not parsed first and replaced by the ClientID..

I had the same clientscript applied to a buttons OnClientClick and there it worked...

Must i apply some sort of attribute to the property to get this working?

here is my code:

Usercontrol.ascx.vb

<ParseChildren(True), PersistChildren(False), Themeable(False)>
Public Class CommandPanel
    Inherits System.Web.UI.UserControl

    Private mClientScript as string
    <Themeable(False), DefaultValue("")> _
    Public Property ClientScript As String
        Get
            Return mClientScript
        End Get
        Set(ByVal value As String)
            mClientScript = value
        End Set
    End Property
End Class

Page.aspx

<%@ Register src="UserControls/CommandPanel.ascx" tagname="CommandPanel" tagprefix="uc1" %>
......
<uc1:CommandPanel ID="CommandPanel1" runat="server" ClientScript="document.getElementsByName('<%= ReportViewer1.ClientId %>$ctl01$ctl07$ctl00$ctl00$ctl00')[0].click(); return false;" />
......

Note: i know that im saving to a local variable and that it will be cleared on reload and so on but thats not the problem here...

回答1:

<%= expressions cannot be used in server-side controls. You can use <%# syntax (then you must call DataBind on user control) or Attributes property.