C# 清除 ASP.NET 中的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:11:39  来源:igfitidea点击:

Clearing a TextBox in ASP.NET

c#asp.net

提问by Hemant Kothiyal

I have webpage where there is textbox having some default value.

我有网页,其中有一些默认值的文本框。

I have to clear the value from text box. I have 2 options

我必须清除文本框中的值。我有2个选择

1. textbox.text="";
2. textbox.text.remove(0,length);

Here i would like to know which one should i use. Does it make any impact on page performance because there are many textbox placed in page.

在这里我想知道我应该使用哪个。因为页面中放置了许多文本框,所以它对页面性能有任何影响吗?

Thanks

谢谢

采纳答案by this. __curious_geek

The best way to do this is..

最好的方法是..

textbox.text = string.Empty;

Also remember that string type is immutable!

还要记住字符串类型是不可变的!

回答by Rex M

It makes no difference - do what is most readable for you and your colleagues.

这没什么区别 - 做对你和你的同事来说最易读的事情。

Many prefer to use string.Empty.

许多人更喜欢使用string.Empty.

回答by flesh

Presumably, you mean clear the value with javascript when a user clicks int the box? If so, it won't make any difference to performance.

据推测,您的意思是当用户单击框时使用 javascript 清除该值?如果是这样,它不会对性能产生任何影响。

I use jQuery in most pages, so I just hook up this function to clear default values onClick, using the ClientId of the textbox:

我在大多数页面中使用 jQuery,所以我只是连接这个函数来清除 onClick 的默认值,使用文本框的 ClientId:

$('#ctl00_TextBox').click(function() { $('#ctl00_TextBox').val('');

If you mean clear it in codebehind use this:

如果您的意思是在代码隐藏中清除它,请使用以下命令:

yourTextBox.Text = String.Empty;

回答by Sam Salisbury

The impact on performance will be minimal even with hundreds/thousands of calls, however

即使有数百/数千个调用,对性能的影响也很小,但是

textbox.text = "";

should in theory be very slightly quicker, since you're just assigning a new value rather than processing the string (as .Remove does)

理论上应该稍微快一点,因为您只是分配一个新值而不是处理字符串(如 .Remove 那样)

The best way to do this would be to do

最好的方法是这样做

textbox.text = String.Empty;

Update:if you're counting clock-cycles, String.Empty will actually execute faster as well, because it doesn't create a new object, whereas "" will create a new String object.

更新:如果你计算时钟周期, String.Empty 实际上也会执行得更快,因为它不会创建一个新对象,而 "" 将创建一个新的 String 对象。

However, you should really not be too concerned about this, there aren't many ways to set a string to empty that will cause performance issues! You should use whichever is the most readable...

但是,您真的不应该太担心这一点,没有很多方法可以将字符串设置为空会导致性能问题!你应该使用最易读的那个......

回答by John Saunders

  1. The performance difference between the two options will be too small to measure, most likely.
  2. TextBox.Text = String.Empty;is a lot more readable. It clearly states what you're trying to do: "set the text property of this text box to an empty string".
  1. 很可能,这两个选项之间的性能差异太小而无法衡量。
  2. TextBox.Text = String.Empty;更具可读性。它清楚地说明了您要执行的操作:“将此文本框的文本属性设置为空字符串”。

I recommend you go with the assignment, as it is both faster, and much more clear.

我建议你去做这个作业,因为它既快又清晰。

回答by venky

textbox.text="";

or

或者

 foreach (var item in Page.Controls)
    {
        if (item is TextBox)
        {
            ((TextBox)item).Text = "";
        }
    }