I have written an ASP.NET server control.

View state works perfectly, but when I'm trying to get a value of a control on my custom control with its public instant method, it brings me an exception that there are not control with that ID.

有帮助吗?

解决方案

If you want to get the values from your custom control, you have to register your controls in OnInit event.

//Register your controls
protected override void OnInit(EventArgs e) {
        var controlName = (Type)LoadControl("~/path.ascx");
        controlName.ID = "YOU_MUST_SET_AN_ID";
        placeholder.Controls.Add(controlName);
}


//get your controls (add the following in any method you like)
var controlNameCtrl = (Type)placeholder.FindControl("CONTROLID");
var propertyValue = controlNameCtrl.PropertyName;

其他提示

When you create a custom control, the page that onward identifies the custom control as one entity and you do not get direct access to individual controls in your custom control.

To get the property value of individual elements of custom control, you should define the properties in your custom control which in turn wraps the individual control inside your custom control.

However, you can always get the value of contained control in the user control itself (Not in the page on which it is placed but in the control code itself.). You can also write events in your custom control to make it interact.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top