Using Code Nuggets to Set Control Properties

Why can't I use code sets to set a control property? For example, a button validation group or a label text property.

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

<asp:Label ID="lbl" runat="server" Text='<% =Test %>' />

      

Is there a way to set control properties without using codebehind?

+2


source to share


2 answers


You can use data binding:

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

      



assuming you call DataBind in the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataBind();
    }
}

      

+3


source


<%=SomeVar %>

uses the latest binding which behaves like Response.Write (in Page.PreRender if I recall correctly). Consequently, the server controls will not be used the way you wanted them to. Unless you are using code binding or inline code to perform the binding.



+1


source







All Articles