[CN.Text开发笔记]OnInit与运行期数据绑定

博主在开发CN.Text时,用<%# DataBinder.EvalContainer.DataItem,\Title\) %>给控件属性赋值未成功,改成普通字符串正常。该属性表示控件标题,在OnInit中显示。将显示属性值代码移至OnPreRender或OnLoad中问题解决,还将用简单例子详细描述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    今天在开发CN.Text的过程中,我遇到了这样一个问题:当我通过<%# DataBinder.EvalContainer.DataItem,"Title"%>给一个控件的属性赋值时, 运行结果却显示这个属性没有被赋值,而我将<%# DataBinder.EvalContainer.DataItem,"Title"%>改成普通的字符串却很正常。这个属性是很简单的一个属性,它表示控件的标题,在控件中只要显示这个属性值就行了。.Text中提供的这个控件是在OnInit中显示这个属性值的。而我只要将显示属性值的代码移至OnPreRender(EventArgs e)或者OnLoad(EventArgs e)中问题就解决了。
    下面我用一个简单的例子详细描述一下这个问题。
    首先,我们定义一个简单的用户控件:

None.gifpublic class MyControl : System.Web.UI.UserControl
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _text="Text is not be set";
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _text
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(Text);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


这个控件很简单,就是将属性Text的值显示出来。

然后我们在一个aspx页面中使用这个控件:
None.gif<asp:Repeater id="Repeater1" runat="server">
None.gif                
<ItemTemplate>
None.gif                    
<uc1:MyControl id="MyControl1" runat="server" Text='<%# "HelloWorld" %>'></uc1:MyControl>
None.gif                
</ItemTemplate>
None.gif            
</asp:Repeater>

None.gifprivate void Page_Load(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            ArrayList al
=new ArrayList();
InBlock.gif            al.Add(
1);
InBlock.gif            al.Add(
2);
InBlock.gif            Repeater1.DataSource
=al;
InBlock.gif            Repeater1.DataBind();
ExpandedBlockEnd.gif        }

运行代码,结果当然是正常的。
现在我们将Response.Write(Text);移到OnInit中
None.gifoverride protected void OnInit(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            Response.Write(Text);
InBlock.gif            InitializeComponent();
InBlock.gif            
base.OnInit(e);
InBlock.gif            
InBlock.gif            
ExpandedBlockEnd.gif        }

运行结果却显示“Text is not be set”, 在执行OnInit时, Text还没有被赋值。而我们只要将Response.Write(Text);移至OnLoad中就能正常显示。

None.gifprotected override void OnLoad(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            Response.Write(Text);
InBlock.gif            
base.OnLoad (e);
ExpandedBlockEnd.gif        }

也就是说, Text是在OnInit之后,OnLoad之前被赋值的。
这就说明Text='<%# "HelloWorld" %>'与Text="HelloWorld"执行的时间是不同的。所以我们在设计控件时,就要注意这一点,假如你想你写的控件支持运行期数据绑定,就避免在OnInit中处理相应的属性值。

转载于:https://www.cnblogs.com/dudu/archive/2004/10/02/48618.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值