Вопрос

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