最近發(fā)現(xiàn)數(shù)據(jù)控件(我這里用Repeater)如果通過手工綁定一個(gè)數(shù)據(jù)源的話,如果回發(fā)時(shí)又一次綁定數(shù)據(jù)的話該數(shù)據(jù)控件里的服務(wù)器控件將不能保存其控件狀態(tài),大致代碼如下:(Repeater控件內(nèi)的TextBox1(TextBox1并沒有綁定數(shù)據(jù),只是在Repeater控件內(nèi)),用Label1來獲取回發(fā)后TextBox1的值)protected void Page_Load(object sender,EventArgs e){? ......這里省略了獲取數(shù)據(jù)的代碼? Repeater1.DataSource=...;? Repeater1.DataBind();}protected void Button1_Click(object sender,EventArgs e){? string str="";? foreach(RepeaterItem item in Repeater1.Items)? {? str+=((TextBox)item.FindControl("TextBox1")).Text; //獲取回發(fā)后TextBox1的值??? }? Label1.text=str;}得到的結(jié)果很明確,就是Repeater1控件內(nèi)的TextBox1在網(wǎng)頁回發(fā)后,如果Repeater1在回發(fā)時(shí)綁定的話,TextBox1將不能保持其在回發(fā)前輸入的值,如果Repeater1在回發(fā)時(shí)不綁定的話,則TextBox1將能保持其在回發(fā)前輸入的值,即改成如下代碼:protected void Page_Load(object sender,EventArgs e){? if(!IsPostBack)? {? ......這里省略了獲取數(shù)據(jù)的代碼? Repeater1.DataSource=...;? Repeater1.DataBind();? }}protected void Button1_Click(object sender,EventArgs e){? string str="";? foreach(RepeaterItem item in Repeater1.Items)? {? str+=((TextBox)item.FindControl("TextBox1")).Text; //獲取回發(fā)后TextBox1的值??? }? Label1.text=str;}如果改成數(shù)據(jù)源控件提供數(shù)據(jù)的話,則TextBox1將能保持其在回發(fā)前輸入的值,也就是和第2種情況一樣。問題:第1種情況下的TextBox1并沒有綁定數(shù)據(jù),為什么在Repeater1在回發(fā)時(shí)綁定數(shù)據(jù)后不能保存其自身的狀態(tài)(即不能保持其在回發(fā)前輸入的值)?數(shù)據(jù)源控件提供數(shù)據(jù)的情況下是在頁面生命周期的哪個(gè)階段為數(shù)據(jù)源控件提供數(shù)據(jù)的? 為什么這種情況下TextBox1將能保持其在回發(fā)前輸入的值? 難道默認(rèn)情況下在回發(fā)時(shí)數(shù)據(jù)源控件將不為數(shù)據(jù)控件提供數(shù)據(jù)?
關(guān)于數(shù)據(jù)控件的數(shù)據(jù)通過手工綁定或通過數(shù)據(jù)源控件綁定的問題
幕布斯6054654
2018-12-07 05:31:43