passing a value to WebUserControl for show in a lable

915 Views Asked by At

hi i have a WebUserControl that have a lable for show message how can i send a value to the lable from Page to my WebUserControl at runtime.

3

There are 3 best solutions below

0
On

You can create a public method in your user control such as

public void ShowMessage(string message)
{
   Label1.Text = message;
}

Label1 being the label control in user control. Now you can use the method from Page as and when you need it - for example,

protected void Page_Load(object Sender, EventArgs e)
{
    MyUserControl1.ShowMessage("Hello");
}

where MyUserControl1 is the name/ID of web user control put on the page.

0
On

In the code behind file of your control you can specify an attribute

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public String customType
    {
        get
        {
            String s = (String)ViewState["customType"];
            return ((s == null) ? String.Empty : s);
        }

        set
        {
            ViewState["customType"] = value;
        }
    }

And after you can get this attribute to fill your label in the pageload with

mylabel.text = mycontrol.customType

In the asp page you specify the attribute (here is the 'customType'):

<wuc:ContSign customType="person" ID="ContSignPanel" runat="server" />

MSDN

0
On

just make a property to get and set values for the lable in the user control

private string _labelmsg;
public string LableMsg
get
{
return _labelmsg;
}
set
{
_labelmsg=lblID.Text;
}

and then set in the aspx.cs page like

UserControlID.LabelMsg="Set Any Value";