asp.net一个小功能的源代码

湖北天马养蜂场2010-04-08 10:23:29Asp.net程序开发5070

源文件里面的代码:

<headrunat="server">
   <title>一个小功能</title>
</head>
<body>
   <formid="form1"runat="server">
**********************************自己写的代码***************************************************************
   <div style="font-size:12px;">
生成的第1个数字:<asp:TextBoxrunat="server"ID="txt1"></asp:TextBox><br/>
生成的第2个数字:<asp:TextBoxrunat="server"ID="txt2"></asp:TextBox><br/>
生成的第3个数字:<asp:TextBoxrunat="server"ID="txt3"></asp:TextBox><br/>
生成的第4个数字:<asp:TextBoxrunat="server"ID="txt4"></asp:TextBox><br/><br/>


输入第1个数字:<asp:TextBoxrunat="server"ID="txtNum1"></asp:TextBox><br/>
输入第2个数字:<asp:TextBoxrunat="server"ID="txtNum2"></asp:TextBox><br/>
输入第3个数字:<asp:TextBoxrunat="server"ID="txtNum3"></asp:TextBox><br/>
输入第4个数字:<asp:TextBoxrunat="server"ID="txtNum4"></asp:TextBox><br/><br/>

<asp:ButtonID="btnOK"runat="server"Text="提交" width="120px"onclick="btnOK_Click"/>
<inputtype="button"id="btnRestart"onclick="location. href='Default.aspx';"value="再玩一次" style="margin-left:10px;"/>
<br/><br/>判断结果:
<asp:LabelID="lblMessage"runat="server"Text=""Font-Size="12"ForeColor="Red"></asp:Label>

<br/><br/>系统计时:
<!--要实现在若干时间系统自动提交,只能(注意是只能!!!)用JS里面的定时器,在下面的text属性里面设置时间并且为什么要用服务器端的TextBox控件,是因为服务器端控件向服务器提交时候,里面的内容不会消失和初始化。-->
<asp:TextBoxID="DivTime"runat="server"Text="60" style=" color:Red;font-family:Arial;font-size:20px;width:50px;"></asp:TextBox>
<script>
functionClock()
{
   if(document.getElementById("DivTime").value=="0"){return;}//如果里面的数字本身就是0了,就不执行下面代码
   varCurrentTime=document.getElementById("DivTime").value;//找到div里面的时间
   document.getElementById("DivTime").value=eval(CurrentTime-1);//将时间减去1秒
   if(document.getElementById("DivTime").value=="0")
   {
       alert("时间到!");
       document.getElementById("btnOK").disabled="disabled";   //按钮不能使用
   }else{}
}
setInterval(Clock,1000);//设置一个JavaScript自带的定时器,1000毫秒即1秒钟执行一次Clock方法
</script>
   </div>
*************************************************************************************************************
   </form>
</body>

cs文件里面的代码:

       protectedvoidPage_Load(objectsender,EventArgse)
       {
           //随机生成4个数字
           if(!IsPostBack)
           {
               //要生成不同的随机数字,那么要种子不同,于是取系统的当前毫秒数来作为种子
               //这个地方很重要,你要仔细看看,很多面试题目都有这种类似的要求
               this.txt1.Text=newRandom(System.DateTime.Now.Millisecond).Next(10).ToString();
               this.txt2.Text=newRandom(System.DateTime.Now.Millisecond+100).Next(10).ToString();
               this.txt3.Text=newRandom(System.DateTime.Now.Millisecond+200).Next(10).ToString();
               this.txt4.Text=newRandom(System.DateTime.Now.Millisecond+300).Next(10).ToString();
           }
           
       }湖北天马养蜂场,加我们的微信一起学养蜂。

       protectedvoidbtnOK_Click(objectsender,EventArgse)
       {
           stringa,b,c,d="";
           stringa1,b1,c1,d1="";
           //取系统生成的值
           a=txt1.Text;
           b=txt2.Text;
           c=txt3.Text;
           d=txt4.Text;
           //取用户输入值
           a1=txtNum1.Text;
           b1=txtNum2.Text;
           c1=txtNum3.Text;
           d1=txtNum4.Text;
           //开始分析,注意此步骤,要是abcd都不是一位的数字,那么就千万不能这样做
           //那么就只能在每2个字母中间和两头加一个“|”符号,要比较的时候...你自己想想该怎么办
           stringtempSystem=a+b+c+d;
           stringresult="";
           //只要包含,就在结果中写一个b
           if(tempSystem.IndexOf(a1)>=0){result+="b";}
           if(tempSystem.IndexOf(b1)>=0){result+="b";}
           if(tempSystem.IndexOf(c1)>=0){result+="b";}
           if(tempSystem.IndexOf(d1)>=0){result+="b";}
           //这个时候,看是否a=a1,b=b1,c=c1,d=d1
           //如果有满足条件的,即将上面的result字符去掉一个b,加上一个a
           //一定要注意,字母a是加在前面,而去掉b是去掉最后一个b,不要搞混了
           if(a==a1){result="a"+result.Substring(0,result.Length-1);}
           if(b==b1){result="a"+result.Substring(0,result.Length-1);}
           if(c==c1){result="a"+result.Substring(0,result.Length-1);}
           if(d==d1){result="a"+result.Substring(0,result.Length-1);}
           //将结果显示在前台即可
           lblMessage.Text=result;
       }

关键测试的是一些字符操作的思路和算法,还有对js的应用。