C.J.
C.J.

Reputation: 3527

Make textbox in UserControl become readonly

I have a couple of textbox in the UserControl. How am I going to change them into readOnly = true/false depending on the conditions?

In my content page, I have the following codes:

<%@ Register TagPrefix="uc1" TagName="item" Src="~/User_Controls/itemDetails.ascx" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent_1" runat="server">
<uc1:item id="ItemList" runat="server"></uc1:item><br />
</asp:Content>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
        if (//Some Conditions)
        {
            //turn ItemList.txtItemName = enable read Only
        }

        if (//Some other Conditions)
        {
            //turn ItemList.txtItemPrice = disable read Only
        }
}

In the ItemDetails.ascx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
    }        

    public string itemName
    {
        get { return txtItemName.Text; }
        set { txtItemName.Text = value; }
    }

    public string itemPrice
    {
        get { return txtItemPrice.Text; }
        set { txtItemPrice.Text = value; }
    }

    //and get...set... for other controls

Upvotes: 0

Views: 1479

Answers (1)

Rashmin Javiya
Rashmin Javiya

Reputation: 5222

Create property in User control ItemDetails.ascx. and assign the property to the textbox enable property.

User control

public bool EnabledtxtItemName { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    txtItemName.Enabled = EnabledtxtItemName;
}

Now you can access the property from respective page you have added the user control.

Web page

protected void Page_Load(object sender, EventArgs e)
{
    ItemList.EnabledtxtItemName = false;
}

Upvotes: 2

Related Questions