ASP.NET TextBox 控件 - 在代码隐藏中获取默认文本值?
我一直在监视 MSDN,但找不到 TextBox 的属性/方法,该属性/方法允许您获取在字段上设置的默认文本值; 我希望能够将当前 txtMyTextBox.Text 与默认值进行比较(如伪代码所示):
var myValue = (String.Compare(txtMyTextBox.Text, txtMyTextBox.DefaultText) ? "" : txtMyTextBox.Text)
这是 ASP.NET 控件中存在的内容吗? 还是我要求太多了? :)
感谢您的帮助(一如既往)!
皮特
I have been spying the MSDN and can't see a property/method for TextBox that allows you to get the default text value that was set on a field; I want to be able to compare the current txtMyTextBox.Text to the default value (like this psuedo code shows):
var myValue = (String.Compare(txtMyTextBox.Text, txtMyTextBox.DefaultText) ? "" : txtMyTextBox.Text)
Is this something which exists in the ASP.NET control? Or am I asking too much? :)
Thanks for any help (as always)!
Pete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
默认文本是指编辑前的初始文本吗?
也许在某个常量/字段/等中声明它,并以编程方式设置它而不是在标记中 - 即在第一次加载中,
txtMyTextBox.Text = defaultText;
- 然后你可以再次比较defaultText
来跟踪更改。By DefaultText do you mean the initial text before editing?
Perhaps declare this in a constant / field / etc somewhere, and set it programatically rather than in the markup - i.e. in the first load,
txtMyTextBox.Text = defaultText;
- then later you can just compare again todefaultText
to track changes.文本框(或任何其他控件)上没有“DefaultText”属性。
您可能已经通过常量字符串定义了默认值,因此只需将 Text 属性与该常量字符串进行比较即可。
There is no "DefaultText" property on a textbox (or any other control).
You probably have defined the default through a constant string, so just compare the Text property to that constant string.
没有内置方法可以在回发期间检索文本框的默认值。
一种选择是使用 ViewState 在初始 PageLoad 期间存储该值,并在回发期间从那里检索该值以进行比较。
There is no built in way of retrieving the default value of a textbox during postback.
One option would be to use ViewState to store the value during the initial PageLoad and retrieving it from there during the postback to make the comparison.
您可以检查的唯一属性是 Text 属性。 如果您需要比较原始值,那么最好将其存储为隐藏字段或会话变量。 然后,您可以对照 textbox.Text 属性中的任何内容进行检查。
The only property you can check is the Text property. If you need to compare an original value then you would be best to store that as maybe a hidden field or session variable. You can then check this against anything in the textbox.Text property.
将原始值放入隐藏字段或视图状态中。
Put the original value in a hidden field or in viewstate.
TextBox 类仅支持 .Text 属性,因此您的“默认”值必须在首次呈现页面之前存储在某个位置,以便您可以在页面回发时检查文本框的 .text 属性。 这个“默认”值可以存储在 cookie 中(如果足够小)、页面的 ViewState 中、页面上的隐藏表单字段中,甚至存储在应用程序或会话状态中。
The TextBox class only supports a .Text property, so your "default" value will have to be stored somewhere in advance of first rendering the page so that you can check the textbox's .text property when the page is posted back. This "default" value could be stored in a cookie (if small enough), in the ViewState of the page, in a hidden form field on the page, or even in the application or session state.
TextBox 没有 DefaultText 属性,所以我很困惑。 您如何设置默认文本值? 如果您只是在代码中设置它,
那么它将是 .Text 属性的值。
TextBox does not have a DefaultText property, so I am confused. How are you setting a default text value? If you are just setting it in the code i.e.
Then it will be the value of the .Text property.