我是一個與ASP.NET生命週期事件混淆的小問題。我有一個複選框,當它被選中時,它會動態地創建標籤和文本框。這是在checkbox oncheckchanged事件中完成的。我在沒有完整回發的複選框上啓用了Ajax。檢查複選框時動態創建的訪問文本框的值
現在在新創建的文本框中輸入值,當我點擊保存按鈕時,在按鈕單擊事件中它甚至不會找到創建的控件。那麼頁面視圖狀態如何記住複選框事件中創建的動態控件,然後在按鈕保存事件中訪問它的值? 標記:在的CheckedChanged事件背後
<tr> <td> <asp:CheckBox ID="chkType" runat="server" Text="Medical Procedure" OnCheckedChanged="ChkMedicalProc_Clicked"></td></tr>
<tr><td colspan="2">
<asp:PlaceHolder ID="dyna" EnableViewState="true" runat="server"></asp:PlaceHolder>
</td></tr>
代碼:
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
Label lbl = new Label();
lbl.Text = string.Empty;
lbl.Text = (_queryParam[i].Param_Name + " (" + _queryParam[i].Param_Type + ") (" + _queryParam[i].Param_Length + ")").ToString();
lbl.Style.Add("font-size", "11px");
lbl.Style.Add("font-family", "Arial");
_txtBox = new TextBox();
_txtBox.ID = ctrlId;
_txtBox.CssClass = "textEntry";
_txtBox.Text = string.Empty;
_txtBox.Text = _queryParam[i].Param_Value;
tc1.Style.Add("width", "21.8%");
tc1.Controls.Add(lbl);
tc2.Controls.Add(_txtBox);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
_tbl.Rows.Add(tr);
this.Master.FindControl("pagecontent1").FindControl("dyna").Controls.Add(_tbl);
保存按鈕單擊事件:
for (int i = 0; i < box.Count; i++)
{
TextBox boxValue= this.Page.Master.FindControl("pagecontent1").FindControl("dyna").FindControl("txtBoxParams-" + i) as TextBox;
//I get object reference error on boxValue
}
您是否在每次回發中創建控件。如果不是這些控件在回發時丟失,並且您無法訪問它們.. –