开发者

How to access the control attribute in UserControl

I add a userControl page. named ModifyUC.ascx, in the control there is a label and textbox. W开发者_开发百科hen I use the userControl in the page named RangeCreate.aspx. I want the set the label and textbox is invisible. When I use the method

protected ModifyUC createUC;
((ExtendedLabel)createUC.FindControl("RangeCodeLable")).Visible = false;//show error
((RadTextBox)createUC.FindControl("RangeCodeText")).Visible = false;

the error is that

(ExtendedLabel)createUC.FindControl("RangeCodeLable") is null. 
so ((ExtendedLabel)createUC.FindControl("RangeCodeLable")).Visible.
Object reference not set to an instance of an object.


This will work out cleaner if you expose RangeCodeLable and RangeCodeText as public properties of ModifyUC, or better, if you expose a public method to hide them both.

Then, when you use ModifyUC elsewhere, just typecast it (if you need to), and then access the public properties/methods.


One thing you can do here is first of all create 2 public properties in your user control's code behind page. Say

public bool RangeCodeLabelVisible {get;set;} public bool RangeCodeTextVisible {get;set;}

and on the page load event of this UC just check these values and make you label and text box visible\invisible.

Now you need to set these properties from the page in which the UC is used. As these properties are public, it would be accessible to you.

Not set the values here and this should work fine.

Rhanks,

Rahul

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜