-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.cs
958 lines (901 loc) · 22.9 KB
/
Utility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI;
namespace DotNet.Utilities
{
/// <summary>
/// Utility 的摘要说明。
/// </summary>
public class Utility:Page
{
#region 数据转换
/// <summary>
/// 返回对象obj的String值,obj为null时返回空值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>字符串。</returns>
public static string ToObjectString(object obj)
{
return null == obj ? String.Empty : obj.ToString();
}
/// <summary>
/// 将对象转换为数值(Int32)类型,转换失败返回-1。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Int32数值。</returns>
public static int ToInt(object obj)
{
try
{
return int.Parse(ToObjectString(obj));
}
catch
{ return -1; }
}
/// <summary>
/// 将对象转换为数值(Int32)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Int32数值。</returns>
public static int ToInt(object obj,int returnValue)
{
try
{
return int.Parse(ToObjectString(obj));
}
catch
{ return returnValue; }
}
/// <summary>
/// 将对象转换为数值(Long)类型,转换失败返回-1。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Long数值。</returns>
public static long ToLong(object obj)
{
try
{
return long.Parse(ToObjectString(obj));
}
catch
{ return -1L; }
}
/// <summary>
/// 将对象转换为数值(Long)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Long数值。</returns>
public static long ToLong(object obj,long returnValue)
{
try
{
return long.Parse(ToObjectString(obj));
}
catch
{ return returnValue; }
}
/// <summary>
/// 将对象转换为数值(Decimal)类型,转换失败返回-1。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Decimal数值。</returns>
public static decimal ToDecimal(object obj)
{
try
{
return decimal.Parse(ToObjectString(obj));
}
catch
{ return -1M; }
}
/// <summary>
/// 将对象转换为数值(Decimal)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Decimal数值。</returns>
public static decimal ToDecimal(object obj,decimal returnValue)
{
try
{
return decimal.Parse(ToObjectString(obj));
}
catch
{ return returnValue; }
}
/// <summary>
/// 将对象转换为数值(Double)类型,转换失败返回-1。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Double数值。</returns>
public static double ToDouble(object obj)
{
try
{
return double.Parse(ToObjectString(obj));
}
catch
{ return -1; }
}
/// <summary>
/// 将对象转换为数值(Double)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Double数值。</returns>
public static double ToDouble(object obj,double returnValue)
{
try
{
return double.Parse(ToObjectString(obj));
}
catch
{ return returnValue; }
}
/// <summary>
/// 将对象转换为数值(Float)类型,转换失败返回-1。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Float数值。</returns>
public static float ToFloat(object obj)
{
try
{
return float.Parse(ToObjectString(obj));
}
catch
{ return -1; }
}
/// <summary>
/// 将对象转换为数值(Float)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Float数值。</returns>
public static float ToFloat(object obj,float returnValue)
{
try
{
return float.Parse(ToObjectString(obj));
}
catch
{ return returnValue; }
}
/// <summary>
/// 将对象转换为数值(DateTime)类型,转换失败返回Now。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>DateTime值。</returns>
public static DateTime ToDateTime(object obj)
{
try
{
DateTime dt = DateTime.Parse(ToObjectString(obj));
if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
return dt;
return DateTime.Now;
}
catch
{ return DateTime.Now; }
}
/// <summary>
/// 将对象转换为数值(DateTime)类型。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>DateTime值。</returns>
public static DateTime ToDateTime(object obj,DateTime returnValue)
{
try
{
DateTime dt = DateTime.Parse(ToObjectString(obj));
if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
return dt;
return returnValue;
}
catch
{ return returnValue; }
}
/// <summary>
/// 从Boolean转换成byte,转换失败返回0。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Byte值。</returns>
public static byte ToByteByBool( object obj )
{
string text = ToObjectString(obj).Trim();
if( text == string.Empty)
return 0;
else
{
try
{
return (byte)(text.ToLower()=="true"?1:0);
}
catch
{
return 0;
}
}
}
/// <summary>
/// 从Boolean转换成byte。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Byte值。</returns>
public static byte ToByteByBool( object obj, byte returnValue )
{
string text = ToObjectString(obj).Trim();
if( text == string.Empty)
return returnValue;
else
{
try
{
return (byte)(text.ToLower()=="true"?1:0);
}
catch
{
return returnValue;
}
}
}
/// <summary>
/// 从byte转换成Boolean,转换失败返回false。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Boolean值。</returns>
public static bool ToBoolByByte( object obj )
{
try
{
string s = ToObjectString(obj).ToLower();
return s == "1" || s== "true"?true:false;
}
catch
{
return false;
}
}
/// <summary>
/// 从byte转换成Boolean。
/// </summary>
/// <param name="obj">对象。</param>
/// <param name="returnValue">转换失败返回该值。</param>
/// <returns>Boolean值。</returns>
public static bool ToBoolByByte( object obj, bool returnValue )
{
try
{
string s = ToObjectString(obj).ToLower();
return s == "1" || s== "true"?true:false;
}
catch
{
return returnValue;
}
}
#endregion
#region 数据判断
/// <summary>
/// 判断文本obj是否为空值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Boolean值。</returns>
public static bool IsEmpty(string obj)
{
return ToObjectString(obj).Trim() == String.Empty ? true : false;
}
/// <summary>
/// 判断对象是否为正确的日期值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Boolean。</returns>
public static bool IsDateTime(object obj)
{
try
{
DateTime dt = DateTime.Parse(ToObjectString(obj));
if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
return true;
return false;
}
catch
{ return false; }
}
/// <summary>
/// 判断对象是否为正确的Int32值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Int32值。</returns>
public static bool IsInt(object obj)
{
try
{
int.Parse(ToObjectString(obj));
return true;
}
catch
{ return false; }
}
/// <summary>
/// 判断对象是否为正确的Long值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Long值。</returns>
public static bool IsLong(object obj)
{
try
{
long.Parse(ToObjectString(obj));
return true;
}
catch
{ return false; }
}
/// <summary>
/// 判断对象是否为正确的Float值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Float值。</returns>
public static bool IsFloat(object obj)
{
try
{
float.Parse(ToObjectString(obj));
return true;
}
catch
{ return false; }
}
/// <summary>
/// 判断对象是否为正确的Double值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Double值。</returns>
public static bool IsDouble(object obj)
{
try
{
double.Parse(ToObjectString(obj));
return true;
}
catch
{ return false; }
}
/// <summary>
/// 判断对象是否为正确的Decimal值。
/// </summary>
/// <param name="obj">对象。</param>
/// <returns>Decimal值。</returns>
public static bool IsDecimal(object obj)
{
try
{
decimal.Parse(ToObjectString(obj));
return true;
}
catch
{ return false; }
}
#endregion
#region 数据操作
/// <summary>
/// 去除字符串的所有空格。
/// </summary>
/// <param name="text">字符串</param>
/// <returns>字符串</returns>
public static string StringTrimAll( string text )
{
string _text = ToObjectString(text);
string returnText = String.Empty;
char[] chars = _text.ToCharArray();
for( int i=0;i<chars.Length;i++)
{
if( chars[i].ToString() != string.Empty)
returnText += chars[i].ToString();
}
return returnText;
}
/// <summary>
/// 去除数值字符串的所有空格。
/// </summary>
/// <param name="numricString">数值字符串</param>
/// <returns>String</returns>
public static string NumricTrimAll( string numricString )
{
string text = ToObjectString(numricString).Trim();
string returnText = String.Empty;
char[] chars = text.ToCharArray();
for( int i=0;i<chars.Length;i++)
{
if( chars[i].ToString() == "+" || chars[i].ToString() == "-" || IsDouble( chars[i].ToString()) )
returnText += chars[i].ToString();
}
return returnText;
}
/// <summary>
/// 在数组中查找匹配对象类型
/// </summary>
/// <param name="array">数组</param>
/// <param name="obj">对象</param>
/// <returns>Boolean</returns>
public static bool ArrayFind(Array array,object obj)
{
bool b = false;
foreach(object obj1 in array)
{
if(obj.Equals(obj1))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 在数组中查找匹配字符串
/// </summary>
/// <param name="array">数组</param>
/// <param name="obj">对象</param>
/// <param name="unUpLower">是否忽略大小写</param>
/// <returns>Boolean</returns>
public static bool ArrayFind(Array array,string obj,bool unUpLower)
{
bool b = false;
foreach(string obj1 in array)
{
if(!unUpLower)
{
if(obj.Trim().Equals(obj1.ToString().Trim()))
{
b = true;
break;
}
}
else
{
if(obj.Trim().ToUpper().Equals(obj1.ToString().Trim().ToUpper()))
{
b = true;
break;
}
}
}
return b;
}
/// <summary>
/// 替换字符串中的单引号。
/// </summary>
/// <param name="inputString">字符串</param>
/// <returns>String</returns>
public static string ReplaceInvertedComma( string inputString )
{
return inputString.Replace("'","''");
}
/// <summary>
/// 判断两个字节数组是否具有相同值.
/// </summary>
/// <param name="bytea">字节1</param>
/// <param name="byteb">字节2</param>
/// <returns>Boolean</returns>
public static bool CompareByteArray(byte[] bytea,byte[] byteb)
{
if(null == bytea || null == byteb)
{
return false;
}
else
{
int aLength = bytea.Length;
int bLength = byteb.Length;
if(aLength != bLength)
return false;
else
{
bool compare = true;
for(int index = 0; index < aLength; index++)
{
if(bytea[index].CompareTo(byteb[index]) != 0)
{
compare = false;
break;
}
}
return compare;
}
}
}
/// <summary>
/// 日期智能生成。
/// </summary>
/// <param name="inputText">字符串</param>
/// <returns>DateTime</returns>
public static string BuildDate( string inputText )
{
try
{
return DateTime.Parse( inputText ).ToShortDateString();
}
catch
{
string text = NumricTrimAll( inputText );
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString();
string day = DateTime.Now.Day.ToString();
int length = text.Length;
if( length == 0 )
return String.Empty;
else
{
if( length<=2 )
day = text;
else if( length<=4 )
{
month = text.Substring(0,2);
day = text.Substring(2,length-2);
}
else if( length<=6 )
{
year = text.Substring(0,4);
month = text.Substring(4,length-4);
}
else if( length>6)
{
year = text.Substring(0,4);
month = text.Substring(4,2);
day = text.Substring(6,length-6);
}
try
{
return DateTime.Parse( year+"-"+month+"-"+day ).ToShortDateString();
}
catch
{
return String.Empty;
}
}
}
}
/// <summary>
/// 检查文件是否真实存在。
/// </summary>
/// <param name="path">文件全名(包括路径)。</param>
/// <returns>Boolean</returns>
public static bool IsFileExists(string path)
{
try
{
return File.Exists(path);
}
catch
{ return false; }
}
/// <summary>
/// 检查目录是否真实存在。
/// </summary>
/// <param name="path">目录路径.</param>
/// <returns>Boolean</returns>
public static bool IsDirectoryExists(string path)
{
try
{
return Directory.Exists(Path.GetDirectoryName(path));
}
catch
{ return false; }
}
/// <summary>
/// 查找文件中是否存在匹配行。
/// </summary>
/// <param name="fi">目标文件.</param>
/// <param name="lineText">要查找的行文本.</param>
/// <param name="lowerUpper">是否区分大小写.</param>
/// <returns>Boolean</returns>
public static bool FindLineTextFromFile(FileInfo fi,string lineText,bool lowerUpper)
{
bool b = false;
try
{
if(fi.Exists)
{
StreamReader sr=new StreamReader(fi.FullName);
string g = "";
do
{
g=sr.ReadLine();
if(lowerUpper)
{
if(ToObjectString(g).Trim() == ToObjectString(lineText).Trim())
{
b = true;
break;
}
}
else
{
if(ToObjectString(g).Trim().ToLower() == ToObjectString(lineText).Trim().ToLower())
{
b = true;
break;
}
}
}
while(sr.Peek()!=-1);
sr.Close();
}
}
catch
{ b =false; }
return b;
}
/// <summary>
/// 判断父子级关系是否正确。
/// </summary>
/// <param name="table">数据表。</param>
/// <param name="columnName">子键列名。</param>
/// <param name="parentColumnName">父键列名。</param>
/// <param name="inputString">子键值。</param>
/// <param name="compareString">父键值。</param>
/// <returns></returns>
public static bool IsRightParent(DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
{
ArrayList array = new ArrayList();
SearchChild(array,table,columnName,parentColumnName,inputString,compareString);
return array.Count == 0;
}
// 内部方法
private static void SearchChild(ArrayList array,DataTable table,string columnName,string parentColumnName,string inputString,string compareString)
{
DataView view = new DataView(table);
view.RowFilter = parentColumnName+"='"+ReplaceInvertedComma(inputString.Trim())+"'";//找出所有的子类。
//查找表中的数据的ID是否与compareString相等,相等返回 false;不相等继续迭代。
for(int index = 0 ;index < view.Count;index ++)
{
if(Utility.ToObjectString(view[index][columnName]).ToLower() == compareString.Trim().ToLower())
{
array.Add("1");
break;
}
else
{
SearchChild(array,table,columnName,parentColumnName,Utility.ToObjectString(view[index][columnName]),compareString);
}
}
}
#endregion
#region 日期
/// <summary>
/// 格式化日期类型,返回字符串
/// </summary>
/// <param name="dtime">日期</param>
/// <param name="s">日期年月日间隔符号</param>
/// <returns></returns>
public static String Fomatdate(DateTime dtime,String s)
{
String datestr="";
datestr=dtime.Year.ToString() + s + dtime.Month.ToString().PadLeft(2,'0')+ s +dtime.Day.ToString().PadLeft(2,'0');
return datestr;
}
/// <summary>
/// 返回日期差
/// </summary>
/// <param name="sdmin">开始日期</param>
/// <param name="sdmax">结束日期</param>
/// <returns>日期差:负数为失败</returns>
public static int Datediff(DateTime sdmin,DateTime sdmax)
{
try
{
double i=0;
while(sdmin.AddDays(i)<sdmax)
{
i++;
}
return Utility.ToInt(i);
}
catch
{
return -1;
}
}
/// <summary>
/// 返回日期差
/// </summary>
/// <param name="sdmin">开始日期</param>
/// <param name="sdmax">结束日期</param>
/// <returns>日期差:负数为失败</returns>
public static int Datediff(String sdmin,String sdmax)
{
try
{
DateTime dmin;
DateTime dmax;
dmin=DateTime.Parse(sdmin);
dmax=DateTime.Parse(sdmax);
double i=0;
while(dmin.AddDays(i)<dmax)
{
i++;
}
return Utility.ToInt(i);
}
catch
{
return -1;
}
}
#endregion
#region 转换用户输入
/// <summary>
/// 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码。
/// </summary>
/// <param name="inputString">用户输入字符串</param>
public static string ConvertStr(string inputString)
{
string retVal=inputString;
//retVal=retVal.Replace("&","&");
retVal=retVal.Replace("\"",""");
retVal=retVal.Replace("<","<");
retVal=retVal.Replace(">",">");
retVal=retVal.Replace(" "," ");
retVal=retVal.Replace(" "," ");
retVal=retVal.Replace("\t"," ");
retVal=retVal.Replace("\r", "<br>");
return retVal;
}
public static string InputText(string inputString)
{
string retVal=inputString;
retVal= ConvertStr(retVal);
retVal=retVal.Replace("[url]", "");
retVal=retVal.Replace("[/url]", "");
return retVal;
}
/// <summary>
/// 将html代码显示在网页上
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static string OutputText(string inputString)
{
string retVal=System.Web.HttpUtility.HtmlDecode(inputString);
retVal=retVal.Replace("<br>","");
retVal=retVal.Replace("&","&;");
retVal=retVal.Replace(""","\"");
retVal=retVal.Replace("<","<");
retVal=retVal.Replace(">",">");
retVal=retVal.Replace(" "," ");
retVal=retVal.Replace(" "," ");
return retVal;
}
public static string ToUrl(string inputString)
{
string retVal=inputString;
retVal= ConvertStr(retVal);
return Regex.Replace(retVal,@"\[url](?<x>[^\]]*)\[/url]",@"<a href=""$1"" target=""_blank"">$1</a>",RegexOptions.IgnoreCase);
}
public static string GetSafeCode(string str)
{
str=str.Replace("'","");
str=str.Replace(char.Parse("34"),' ');
str=str.Replace(";","");
return str;
}
#endregion
//#region 弹出框
///// <summary>
///// 服务器端弹出alert对话框
///// </summary>
///// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
///// <param name="page">Page类</param>
//public static void Alert(string str_Message,Page page)
//{
// page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
//}
///// <summary>
///// 服务器端弹出alert对话框
///// </summary>
///// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
///// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
///// <param name="page">Page类</param>
//public static void Alert(string str_Ctl_Name,string str_Message,Page page)
//{
// page.RegisterStartupScript("","<script>alert('"+str_Message+"');document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
//}
///// <summary>
///// 服务器端弹出confirm对话框,该函数有个弊端,必须放到响应事件的最后,目前没有妥善解决方案
///// </summary>
///// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
///// <param name="btn">隐藏Botton按钮Id值,比如:btn_Flow</param>
///// <param name="page">Page类</param>
//public static void Confirm(string str_Message,string btn,Page page)
//{
// page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn+".click();}</script>");
//}
///// <summary>
///// 服务器端弹出confirm对话框,询问用户准备转向相应操作,包括“确定”和“取消”时的操作
///// </summary>
///// <param name="str_Message">提示信息,比如:"成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
///// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
///// <param name="btn_Redirect_Self">"取消"按钮id值</param>
///// <param name="page">Page类</param>
//public static void Confirm(string str_Message,string btn_Redirect_Flow,string btn_Redirect_Self,Page page)
//{
// page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn_Redirect_Flow+".click();}else{document.forms(0)."+btn_Redirect_Self+".click();}</script>");
//}
//#endregion
/// <summary>
/// 设置绑定到DataGrid的DataTable的记录行数,如不够则添加空行
/// </summary>
/// <param name="myDataTable">数据表</param>
/// <param name="intPageCount">DataGrid分页时每页行数</param>
public static void SetTableRows(DataTable myDataTable,int intPageCount)
{
try
{
int intTemp=myDataTable.Rows.Count%intPageCount;
if ((myDataTable.Rows.Count==0) || (intTemp!=0))
{
for(int i=0;i<(intPageCount-intTemp);i++)
{
DataRow myDataRow=myDataTable.NewRow();
myDataTable.Rows.Add(myDataRow);
}
}
}
catch
{
throw;
}
}
public static string GetGuid(string guid)
{
return guid.Replace("-", "");
}
public static string ReadConfig(string filePath)
{
return System.Configuration.ConfigurationManager.AppSettings[filePath];
}
#region 字符串长度区分中英文截取
/// <summary>
/// 截取文本,区分中英文字符,中文算两个长度,英文算一个长度
/// </summary>
/// <param name="str">待截取的字符串</param>
/// <param name="length">需计算长度的字符串</param>
/// <returns>string</returns>
public static string GetSubString(string str, int length)
{
string temp = str;
int j = 0;
int k = 0;
for (int i = 0; i < temp.Length; i++)
{
if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
{
j += 2;
}
else
{
j += 1;
}
if (j <= length)
{
k += 1;
}
if (j > length)
{
return temp.Substring(0, k) + "...";
}
}
return temp;
}
#endregion
}
}