的FindControl会在FormView控件不正确的模板对照 [英] FindControl looks for controls in incorrect template of FormView

查看:366
本文介绍了的FindControl会在FormView控件不正确的模板对照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何从codebehind在一个FormView切换模式时,定位控制?
它看起来像你不能Page_Load事件过程中使用的FindControl,因为它将搜索在$ p $控制pviously所示的模板,而不是新选择之一。
我怀疑你不能依靠单独pageLoad的但不得不另找事件中控件,比如OnDataBound,但你要真有这样做呢?
我已经看到了我的日子,缺乏像OnDataBound ...

How do you locate controls from the codebehind when switching modes in a FormView? It looks like you can't use FindControl during the Page_Load event, since it will be searching for controls in the previously shown template rather than the newly selected one. I suspect you can't rely on the PageLoad alone but have to find controls within another event, like OnDataBound, but should you really HAVE to do that? I've seen several formviews in my day that lack events like OnDataBound...

关于我的具体情况的更多细节:
我有一个FormView这里既有的ItemTemplate,InsertItemTemplate元素和EditItemTemplate中包含相同的文本框。 (它有在所有模板相同的ID)

More details about my specific case: I've got a formview where both the ItemTemplate, InsertItemTemplate and EditItemTemplate contain the same textbox. (it's got the same ID in all templates)

在我使用的FindControl找到文本框,改变它的知名度Page_Load事件。
工作完全正常,当最初加载FormView控件,但由于某些原因,当窗体改变模式/更改模板(在页面呈现后,你看到文本框的可见性是不正确),这是行不通的。

During the Page_Load event I use FindControl to locate the textbox and change it's visibility. Works perfectly fine when initially loading the formview, but for some reason it doesn't work when the form is changing modes/changing templates (after the page has rendered you see that the textbox visibility is incorrect)

有关例如从读切换到编辑模式 - formview.Mode将设置为FormViewMode.Edit,但pageLoad的活动期间使用的FindControl时将搜索的ItemTemplate而非EditItemTemplate模板内的控制。
因此,如果你有一个在所有模板相同ID的控制它会找到不正确的模板中的控制,并在页面加载后,你会以为什么的加载控件不具有相同的属性极其混乱因为你认为它,当你在页面加载过程中调试检查它。

For instance switching from read to edit mode - the formview.Mode will be set to FormViewMode.Edit, but when using FindControl during the PageLoad event it will search for controls within the ItemTemplate rather than the EditItemTemplate. Thus, if you have a control with the same ID in all templates it will find the control within the incorrect template, and after the page has loaded you'll be extremely confused as to why the control that's loaded doesn't have the same properties as you thought it when you examined it in the debugger during the pageLoad.

推荐答案

不要使用的Page_Load 绑定或访问您的 FormView控件,而是使用的 FormView控件 数据绑定事件和的 CURRENTMODE 属性

Don't use Page_Load to bind or access your FormView, instead use the FormView's DataBound event and the CurrentMode property:

protected void FormView1_DataBound(object sender, System.EventArgs e)
{
    if(FormView1.CurrentMode == FormViewMode.ReadOnly)
    {
        // here you can safely access the FormView's ItemTemplate and it's controls via FindControl
    }
    else if(FormView1.CurrentMode == FormViewMode.Edit)
    {
        // here  you can safely access the FormView's EditItemTemplate and it's controls via FindControl
    }
    else if(FormView1.CurrentMode == FormViewMode.Insert)
    {
        // here you can safely access the FormView's InsertItemTemplate and it's controls via FindControl
    }
}

这篇关于的FindControl会在FormView控件不正确的模板对照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆