简体   繁体   English

具有CalendarExtender的TextBox的.Text属性始终为空

[英].Text property of TextBox with CalendarExtender is always empty

It's been a long time since I've worked on asp.net web forms, and I need a little refresher. 自从我从事过asp.net Web表单以来已经很长时间了,我需要复习一下。 I'm doing some mods to an old app... 我正在对旧应用程序进行一些修改...

<asp:TextBox 
    ID="Text_DatePerformed" 
    runat="server" 
    CssClass="style_default" 
    Font-Size="8pt" 
    Width="75px" 
    ReadOnly="true">
</asp:TextBox>

<asp:CalendarExtender 
    ID="CalendarExtender2" 
    TargetControlID="Text_DatePerformed" 
    Format="MM/dd/yyyy" 
    runat="server" />

This appears to be working in the UI. 这似乎在用户界面中起作用。 If I click in the textbox the date-picker pops up, I can pick a date, and the correct date appears in the textbox. 如果单击文本框,则会弹出日期选择器,我可以选择一个日期,然后在文本框中显示正确的日期。 But, trouble I'm having is, in the code-behind, the .Text attribute always comes up empty. 但是,我遇到的麻烦是,在代码背后, .Text属性始终为空。

string DatePerformed = Text_DatePerformed.Text;

Set readonly=false otherwise ViewState doesn't persist it. 设置readonly=false否则ViewState不会保留它。

<asp:TextBox 
    ID="Text_DatePerformed" 
    runat="server" 
    CssClass="style_default" 
    Font-Size="8pt" 
    Width="75px" 
    ReadOnly="false">
</asp:TextBox>

If you need it you either have to make it readonly at clientside or set it on Page_Load or later: 如果需要,则必须在客户端将其设置为只读,或者在Page_Load或更高版本中进行设置:

 Text_DatePerformed.Attributes.Add("readonly", "readonly");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM