代码之家  ›  专栏  ›  技术社区  ›  TestSubject09

使用代码块设置控件属性

  •  2
  • TestSubject09  · 技术社区  · 15 年前

    为什么不能使用代码块设置控件属性??例如按钮的验证组或标签的文本属性。

    <asp:Button ID="btn" runat="server" Text="test" ValidationGroup='<% =TestValidate %>'
    
    <asp:Label ID="lbl" runat="server" Text='<% =Test %>' />
    

    是否有任何方法可以在不使用codebehind的情况下设置控件属性?

    2 回复  |  直到 15 年前
        1
  •  3
  •   Darin Dimitrov    15 年前

    您可以使用数据绑定:

    <asp:Label ID="lbl" runat="server" Text='<%# "Hello World" %>' />
    

    如果你打电话来 DataBind 代码背后:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }
    
        2
  •  1
  •   o.k.w    15 年前

    <%=SomeVar %> 使用行为类似于response.write的后期绑定(如果我记得正确,请在page.prerender中)。因此,服务器控件不会像您希望的那样使用它。除非使用代码隐藏或内联代码隐藏来执行绑定。