stackuser
stackuser

Reputation: 672

Textbox.text returning blank

I have a textbox(txtShipmentDate) that is getting populated with a value selected from a pop-up calendar in the .aspx file.

<tr>
    <td align="right" valign="middle" width="125">
       Shipment Date:
    </td>                                       
    <td align="left" valign="middle">
       <asp:TextBox Runat="server" ReadOnly="true" Width="75" 
           ID="txtShipmentDate" CssClass="textbox"></asp:TextBox>
       <a title="Select Ship Date" onclick="if(self.gfPop)gfPop.fPopCalendar(document.forms[0].txtShipmentDate);return false;"
href="javascript:void(0)">
       <IMG class="PopcalTrigger" height="19" alt="" src="/TOrders/calendarxp/themes/xp/calbtn.gif" width="34" align="absMiddle" border="0"></a>
    </td>
</tr>

The textbox gets populated correctly.

But when I try to access the value in the textbox when the Submit button is clicked, using

     If txtShipmentDate.Text.Length > 0 Then oShippingDate = Convert.ToDateTime(txtShipmentDate.Text)

the value in the texbox is blank. There is nothing else hapenning before the submit. All other textboxes retain their values.

Upvotes: 1

Views: 1290

Answers (1)

suff trek
suff trek

Reputation: 39767

Due to security precautions ReadOnly fields do not persist their values upon postback. If you need to prevent user input into this field and still post the value back - do not set textbox'es ReadOnly property. Instead give it a ReadOnly appearance via CSS and then prevent user input thru something like

txtShipmentDate.Attributes("onclick") = "this.blur();"

(You may want to handle 'onselect' and 'onpaste' as well). This way user input is prevented, giving user complete ReadOnly field appearance and still value is posted back.

Upvotes: 1

Related Questions