在文本框中自动生成号码 [英] Auto number genrate in textbox

查看:282
本文介绍了在文本框中自动生成号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从1开始自动生成文本框中的ID?
如果我们在另一个文本框中输入产品名称,则产品ID为1.
然后添加另一个产品名称,然后autoallall id赋值为2....

how to auto genrate ID in textbox start from 1?
if we write the product name in another textbox then product id is 1.
and i add another product name then automaticall id asign is 2....

推荐答案

在最简单的情况下,您可以有一个静态字段(uint可以很好)的某个类,该类将以1初始化并且可以通过属性访问,就像每个请求将增加值的方式一样:
In a simplest case, you could have a static field (uint would do fine) of some class which would initialized with 1 and be accessible from a property the way each request would increment the value:
internal uint Id {
   get { return id++; }
}
private static id = 1;



当心:此代码不是线程安全的.如果需要从不同的线程访问Id,请确保使用lock:
进行操作. http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [ ^ ].

现在,使用静态方法需要格外小心,最好避免使用.如果声明的类每次运行仅具有一个实例,则此字段可能不是静态的.如果不必一定是 singleton ,最好不要在一个地方使用此Id,但是 singleton模式是最典型的通用名称方法.请参阅:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial [^ ].

我看到了许多糟糕的单例实现.对于C#,这是一个不错的选择:
http://csharpindepth.com/Articles/General/Singleton.aspx [



Beware: this code is not thread safe. If you need to access Id from different threads, make sure you do it with lock:
http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

Now, working with static needs extra care and better be avoided. This field may not be static if the declared class has only one instance per run time, guaranteed. If does not have to be a singleton, and it better should not, if you use this Id only in one place, but the singleton pattern is the most typical universal approach to that. Please see:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

See also:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^].

I saw many bad singleton implementations. This is a good one, for C#:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA




希望您正在做一些与数据库有关的工作.如果要在数据库中插入条目,则不应在C#中编写ID的代码,而应在数据库中添加AutoIncrement列.

如果您编写用于从C#生成ID值的代码,则可能会出现更多问题.您暂时不会理解这些问题.

在本文中查找以下内容:
SQL中的自动生成列服务器 [
Hi,

Hope you are doing some work related with the database. If you are inserting entry in the database then you should not write code for ID in the C# instead you should add AutoIncrement column in the database.

If you write your code for generating ID value from C# then there are more issues may come. you will not understand those issue for now.

Check this article for : Autogenerated Column in SQL Server[^]

When you are adding some value in the name textbox generate one dummy value in the database and that will gives you one ID from it. If user press save button then update it otherwise discard that value. this way you can do that.

Although SA gives really good examples to do the same from C# but this can be other way to do your work.

Hope this information helps you,

Thanks
-Amit Gajjar.


这篇关于在文本框中自动生成号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆