User Controls Asp .Net

928 views Asked by At

Task – develop user control, which has <%#Bind(“expression”)%>`

How send parametrs into user control and how to use Bind and Eval ?

1

There are 1 answers

0
Joe Alfano On

There is a good tutorial about creating user controls and setting their properties in this MSDN Article.

Basically, you create an ascx page and its code-behind. In the code-behind, you create the properties that you want exposed and store their values using ViewState. It is important for your property backing store to be viewstate for reasons discussed in this article:

public partial class MyControl : System.Web.UI.UserControl
{
    public DateTime BeginDate 
    {
        get { return (DateTime)(ViewState["BeginDate"] ?? new DateTime()); }
        set { ViewState["BeginDate"] = value; }
    }
    ......

The after you register this new user control in your web.config file, you can declarative place an instance of the control in your hosting page and set its property:

<Custom:MyControl Id="Mycontrol" runat="server" BeginDate ="2012-01-26" />

If you want to use databinding to set the value of the control, simply assign the value of the property to a databinding expression:

<Custom:MyControl Id="Mycontrol" runat="server" BeginDate ='<%# GetBeginDate() %>' />

Where GetBeginDate() is a public or protected method in your code behind page that returns a date.

You can see this article for a good description of the basics of databinding.