Problem with getting textbox value
-
when i pressed button add, the following code would add a text box to datagrid TextBox txtEdit = new TextBox(); dgTest.Items[0].Cells[0].Controls.Add( txtEdit ); After that i pressed button delete, the following would remove the textbox from datagrid TextBox txtEdit = ( TextBox ) dgTest.Items[0].Cells[0].FindControl("txtEdit"); // lblTest.Text = txtEdit.Text dgTest.Items[0].Cells[0].Controls.Remove( txtEdit ); What i can not understand is that it removed the textbox sucessfully that means it could find the textbox but it can't not get the text that i typed in the textbox Thanks
-
when i pressed button add, the following code would add a text box to datagrid TextBox txtEdit = new TextBox(); dgTest.Items[0].Cells[0].Controls.Add( txtEdit ); After that i pressed button delete, the following would remove the textbox from datagrid TextBox txtEdit = ( TextBox ) dgTest.Items[0].Cells[0].FindControl("txtEdit"); // lblTest.Text = txtEdit.Text dgTest.Items[0].Cells[0].Controls.Remove( txtEdit ); What i can not understand is that it removed the textbox sucessfully that means it could find the textbox but it can't not get the text that i typed in the textbox Thanks
the problem is most probably related to ViewState ~ when controls are added dynamically to a page ~ then its viewstate is not saved ~ if you see the rendered html of the page, a hidden field __viewstate is present which stores the viewstate information of all the controls in the page ~ when controls are added dynamically , its viewstate information is not available ~ what can be done is after you add the textbox, try saving it in the StateBag ~ in the page load event ~ retrieve the textbox from the StateBag and add it in the same location again ~ you will find that when the control reaches your post back event handler the text in the text box is now available