有一自定义控件文件AbuttonControl.ascx
...
<INPUT type="button" value="abc" name="Submit1" runat="server"/>
...
有一WEB页面default.aspx
<%@ Register TagPrefix="Control" TagName="Abutton" Src="AbuttonControl.ascx" %>
...
<Control:Abutton id="button1" value="xyz" runat="server"></Control:Abutton>
...
但我不能动态地改变按钮中显示的文字(abc不能改变为xyz)
请教怎样才能改变?

解决方案 »

  1.   

    在用户控件中定义属性来维持值,就可改了
    Property.ascx:
    <script language="C#" runat="server">
      public String Color = "blue";
      public String Text = "This is a simple message user control!";
    </script>
    <span id="Message" style="color:<%=Color%>"><%=Text%></span>Property.aspx
    <%@ Register TagPrefix="Sodi" TagName="Message" Src="Property.ascx" %>
    <html>
      <script language="C#" runat="server">
          void SubmitBtn_Click(Object sender, EventArgs E) {
              MyMessage.Text = "Message text changed!";
              MyMessage.Color = "red";
          }
      </script>
    <body style="font: 10pt verdana">
      <h3>A Simple User Control w/ Properties</h3>
      <form runat="server">
        <Sodi:Message id="MyMessage" Text="This is a custom message!" Color="blue" runat="server"/>
        <p>
        <asp:button text="Change Properties" OnClick="SubmitBtn_Click" runat=server/>
      </form>
    </body>
    </html>
      

  2.   

    in normal cases, don't use <%=...%>, it is not a good practice, after all, you are in ASP.NET world now, not ASP worldadd a code behind to your ascx<%@ Control Inherits="YourNS.YourUC" CodeBehind="AbuttonControl.ascx.cs"%>
    <INPUT type="button" value="abc" id="Submit1" runat="server"/>
    AbuttonControl.ascx.cs
    ..namespace YourNS
    public class YourUC : UserControl
    {
      protected HtmlInputButton Submit1;
      public string Value
      {
    get { return Submit1.Value;}
            set { Submit1.Value = value;}
      }}
    then in default.aspxprotected YourNS.YourUC button1;
    button1.Value = "abc";