Custom ITemplate中的FindControl

时间:2012-05-28 15:01:52

标签: c# asp.net .net templates itemplate

我有一个使用简单ITemplate的自定义UserControl:

<asp:Panel runat="server" ID="pnlExpander" CssClass="expander">
    <asp:HyperLink runat="server" ID="lnkExpand" Text="More Options" NavigateUrl="#" CssClass="lnkExpand"/>
    <asp:Panel runat="server" ID="pnlContent" CssClass="expanderContent" style="display: none">
        <asp:PlaceHolder runat="server" ID="plcContent"/>
    </asp:Panel>
</asp:Panel>

模板使用两个简单属性进行渲染:

public class Expander {
    private ITemplate _contentTemplate;
        public ITemplate ContentTemplate {
            get { return _contentTemplate; }
            set { _contentTemplate = value; }
    }
    protected override void OnPreRender(EventArgs e) {
            if (ContentTemplate != null) {
                ContentTemplate.InstantiateIn(plcContent);
            }
    }

一切都正确显示,但我无法在模板中使用FindControl。我从VS intellisense获得了对我的组合框的引用,但是编译错误,它实际上是加载页面。

要在模板中找到组合框,我正在使用:

var cboFilterCriticality = AspNetUtils.FindControlRecursive(optionsExpander,"cboFilterCriticality") as DropDownList;

实际模板在页面上显示如下:

<l49:Expander runat="server" ID="optionsExpander">
    <ContentTemplate>
        ... other controls
            <asp:DropDownList ID="cboFilterCriticality" runat="server" ValidationGroup="filterGrid" DataTextField="Key" DataValueField="Value" />
    </ContentTemplate>
</l49:Expander>

2 个答案:

答案 0 :(得分:1)

我通过更改使用ITemplate的UserControl解决了这个问题。出于某种原因,它在InstantiateIn中调用OnPreRender,显然为时已晚,无法通过页面中的Page_Load提取任何内容 - 请参阅Page LifeCycle and UserControls (half way down)。我在UserControl中将InstantiateIn移到OnInit,问题解决了。

答案 1 :(得分:0)

Asp.net的WebForm页面:

public class Expander { 
    public ITemplate ContentTemplate {get ;set;}
    public HtmlGenericControl ContentTemplateContainer{get;set;}
    protected override void OnInit(EventArgs e) {
        this.ContentTemplateContainer = new HtmlGenericControl("div");
        if (ContentTemplate != null) {
            ContentTemplate.InstantiateIn(container);
        }
        plcContent.Controls.Add(container);

    }
}

将Expander类定义如下:

public override void OnInit(EventArgs e){
    base.OnInit(e);
    ViewState["ContentTemplateContainerID"] =  ContentTemplateContainer.ClientID;
}
页面的OnInit中的

var containerID = ViewState("ContentTemplateContainerID");
var elID = $get(containerID)[0].id;
var expander = $find(elID);

最后在Javascript中:

- (id)init {
self = [super init];
self.locationMeasurements = [NSMutableArray array];

_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[self stopMonitorSpecificRegion];

if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [_locationManager requestWhenInUseAuthorization];
}

_locationManager.distanceFilter = [[NSUserDefaults standardUserDefaults] valueForKey:@"radialDistance"]?[[[NSUserDefaults standardUserDefaults] valueForKey:@"radialDistance"] doubleValue]:kCLDistanceFilterNone;                          // whenever we move
_locationManager.desiredAccuracy = [[NSUserDefaults standardUserDefaults] valueForKey:@"desiredAccuracy"]?[[[NSUserDefaults standardUserDefaults] valueForKey:@"desiredAccuracy"] doubleValue]:kCLLocationAccuracyHundredMeters;
return self;