问题来自论坛:http://topic.csdn.net/u/20120515/08/d429714e-523b-4dd1-ad9a-c107bc2fc11b.html
下面是Insus.NET试作演示:

演示源程代码:
.NET Framework 4.5 + C# + asp.net:
http://download.cnblogs.com/insus/ASPDOTNET/LoadUserControl.zip
老师布置的另外一道题是将字符串"a;b;d;z;y;u"切割成阵列并排序列出。老师出这题也许是让Insus.NET掌握或复习Array.Sort()函数,不管怎样,先按自己的想法实现,然后是等老师的意见。
{
string s = "a;b;d;z;y;u";
string[] sa = s.Split(';');
Array.Sort(sa); //排序
for (int i = 0; i < sa.Length; i++)
{
Response.Write(sa[i].ToString() + "<br />");
}
}
执行结果:
老师布置给Insus.NET第四道题目。一开始时,是使用下面的方法解答:
{
Response.Write(string.Format("数字1~10总和等于{0}。", Sum(1, 10).ToString()));
}
private int Sum(int min, int max)
{
int s = 0;
for (int i = min; i <= max; i++)
{
s += i;
}
return s;
}
执行结果:
上面的方法,Insus.NET觉得不理想,故产生如下另外一个方法,重构了上面的Sum的函数:
{
return (max + 1) * max / 2;
}
这是老师布置Insus.NET的第三道题目。以下是Insus.NET的答案,仅供参考:
{
//指定目标文件夹
string directory = @"C:\Windows\Microsoft.NET\Framework\v3.5";
IterationFile(directory);
}
private void IterationFile(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
//输出当前目录。
Response.Write(di.ToString() + "<br />");
//取得当前目录中所有文件
FileInfo[] fiArray = di.GetFiles();
//循环每一个文件
for (int i = 0; i < fiArray.Length; i++)
{
Response.Write(fiArray[i].ToString() + "<br/>");
}
//每个目录结束,写一空行。
Response.Write("----------------------------------------------------------------------------<br/>");
//取得当前目录中所有子目录
DirectoryInfo[] diArray = di.GetDirectories();
//循环每一个目录
for (int j = 0; j < diArray.Length; j++)
{
IterationFile(diArray[j].FullName);
}
}
老师布置Insus.NET做的第二道题,题目如标题。
感兴趣的网友也可以练习练习。现在Insus.NET的作答如下,但老师还没有看,因此答案是否正确或是最好的,还不能确定,只是供参考。
for (int i = 1; i <= max; i++)
{
int j = i * i;
Response.Write(i + " × " + i + " = " + j + "<br />");
}
结果:
老师布置作业给Insus.NET做,题目如标题。
感兴趣的网友也可以练习练习。现在Insus.NET的作答如下,但老师还没有看,因此答案是否正确或是最好的,还不能确定,只是供参考。
第一次以最快的方法来实现:
{
int j = i * i;
if (j >= 50)
Response.Write(i + " × " + i + " = " + j + "<br />");
}
得到结果如下:
在上面完成之后,Insus.NET马上想起,老师一定不会出这样简单的问题的。会不会老师要Insus.NET实现,积小于50的两个数字相乘的不必循环呢,减轻性能,因此马上修改刚才完成的程序:
首先求出50的平方根数为多少,得到的数为Double的数据类型,也就是说有可以是小数出现,把它转换为Integer:
这样,我们就可以知道是从哪一个Integer开始进行循环。但是某一天,不再是计算积为50,而是改为积64的话,它的平方根为8。如果以8的平方刚好是64,而题目是要大于64的,因此正确是从9开始。故还是下面的判断:
min += 1;
最终Insus.NET的答案如下:
{
int j = i * i;
Response.Write(i + " × " + i + " = " + j + "<br />");
}
结果还是同第一次的结果一样。
比如一个字符串"a,b,a,c,b,b,d",现在我们要统计每个字符串出现次数。解决这个问题,我们可以使用泛型集合 Dictionary(TKey,TValue)。它有一个key值用来存储字符串和一个value值,用来存储字符串出现的次数。
实现第一步,需要把字符串分割为一个array,需要使用到的函数Split():
第二步,用Dictionary(TKey,TValue)实例化。
第三步,统计:
{
if (Statistics.ContainsKey(w))
{
Statistics[w] += 1;
}
else
{
Statistics[w] = 1;
}
}
写完以上代码算是大功告成。
但Insus.NET还是要把统计的结果显示出来:
.aspx:
View Code <HeaderTemplate>
<table border="1" cellpadding="1" cellspacing="0">
<tr>
<td>字符 </td>
<td>次数 </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("key") %>
</td>
<td>
<%# Eval("value") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.aspx.cs:
View Code {
this.Repeater1.DataSource = Statistics;
this.Repeater1.DataBind();
}
结果:
如果你想看看MS SQL Server版本: http://www.cnblogs.com/insus/archive/2012/02/23/2364580.html
本演示是让你知道如何多个值绑定至ListBox显示为选中。ListBox在default情况之下,SelectionMode为Single,因此为了多选,而需要设置此属性为Multiple。在实现之前,可以先看到Insus.NET所实现最终效果,gif动画,无声音:
.aspx:
<br />
<asp:Button ID="Button1" runat="server" Text="Binding" OnClick="Button1_Click" />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="100" SelectionMode="Multiple" ></asp:ListBox>
.aspx.cs中,首先是为ListBox准备数据,然后对ListBox控件进行数据绑定:
View Code {
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
this.ListBox1.DataSource = Site();
this.ListBox1.DataTextField = "key";
this.ListBox1.DataValueField = "value";
this.ListBox1.DataBind();
}
private Dictionary<string, string> Site()
{
Dictionary<string, string> site = new Dictionary<string, string>();
site.Add("Insus.NET cnblogs", "http://insus.cnblogs.com");
site.Add("Microsoft", "http://www.microsoft.com");
site.Add("Google", "http://www.google.com");
site.Add("Yahoo", "http://www.yahoo.com.cn");
site.Add("Ifeng", "http://www.ifeng.com");
site.Add("sina", "http://www.sina.com.cn");
site.Add("163", "http://www.163.com");
site.Add("QQ", "http://www.qq.com");
return site;
}
为了让TextBox的字符串以";"分割为多个值,引用了命名空间
接下来,是写button的click事件,代码相当简单,Insus.NET在此不作过多注释:
View Code {
string[] s = this.TextBox1.Text.Split(';');
foreach (ListItem li in this.ListBox1.Items)
{
li.Selected = ((IList)s).Contains(li.Text) ? true : false;
}
}
专案需要,一个TextBox要存储时间值。由于asp.net的Validation的验证工具的asp:CompareValidator只能验到日期。验证日期可以参考:http://www.cnblogs.com/insus/archive/2012/03/24/2415171.html
如何判断用户在TextBox输入的字符串就是一个有效的时间呢? Insus.NET解决方法并没有使用正则表达式,也可以实现到相同验证效果,没有警告提示,说明输入的字符串是一个有效的时间字符串:
.aspx,放置一个TextBox, 写OnTextChanged事件,还要设置AutoPostack=“true”属性。
.aspx.cs:
{
var tb = sender as TextBox;
//ConverData类别,可以参考:http://www.cnblogs.com/insus/articles/1424094.html
if (ConvertData.ToDateTime(DateTime.Today.ToString("yyyy-MM-dd") + " " + tb.Text.Trim()) == null)
{
//以下类别,参考:http://www.cnblogs.com/insus/articles/1341703.html
InsusJsUtility objInsusJsUtility = new InsusJsUtility();
objInsusJsUtility.JsAlert("输入字符串不是有效时间。");
return;
}
}




