asp TextBox Text property is always empty on server-side

Problem: when I add an asp:TextBox to a complex SharePoint 2010 page,
the javascript to set the value of the asp:TextBox worked fine,
however on server-side, the TextBox.Text property was always empty.

note: I test the javascript, using the Console in Google Chrome browser (hint: press CTRL + SHIFT + I).

this is the TextBox:

   <!-- hidden text boxes, for passing IDs to/from server   
     NB: make sure this sits inside an existing Form element!  
     -->  
   <asp:TextBox ID="txtIdsForGridkendoGrid1" idforclient="txtIdsForGridkendoGrid1" style="display:none;" TextMode="MultiLine" Wrap="false" CssClass="" Rows="16" Width="97%" runat="server" />  

this is the javascript to set the value of the TextBox: (I am sure there is a more elegant way to write this!)


       var gridId = 'kendoGrid1';
       var selectorVal = "textarea[idforclient='txtIdsForGrid" + gridId + "']:first";  
       jQuery(selectorVal).each(function () {  
         $(this).val(idsSelectedListInString);  
       });  

Cause:  when I viewed the source of the page in a browser, I could see that although there was a Form on the page, the TextBox was sitting outside of the Form element.

Solution: I moved the asp:TextBox to a asp:ContentPane where I knew it would appear inside the Form.

The TextBox.Text property value then appeared OK on server side.

Other possible causes:
- resetting TextBox.Text value server side
- performing a page load (in Page_Load() we should check for IsPostBack)
- EnableViewState set to false in the page (the default is true)
- web.config settings ?

Comments