4递归实现阶乘计算器
aspx:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server"
Text="的阶乘="></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button
ID="Button1" runat="server" Text="计算" Xοnclick="Button1_Click"
Width="57px" />
aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text= multiply(Convert.ToInt32(TextBox1.Text)).ToString();
}
public static int multiply(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * (multiply(n - 1));
}
}
5递归实现TreeView绑定表MenuTree
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace TreeViewDemo
{
public partial class TreeDemo3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindTree();
}
private void bindTree()
{
DataTable dt = this.GetTreeData();
this.FillNode(dt, null);
}
private void FillNode(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
if (node == null) //根结点
{
dv.RowFilter = "parentid='0'"; //筛选到根结点的所有子节点
}
else //不是根结点
{
dv.RowFilter = "parentid='" + node.Value + "'"; //筛选对应节点的所有子节点
}
foreach (DataRowView drv in dv) //遍历填充节点的所有子节点,如果传入的节点node为叶子节点,遍历要退出,不再进行递归
{
TreeNode no = new TreeNode(drv["menuname"].ToString(), drv["menuid"].ToString());
FillNode(dt, no); //填充no节点的子节点
if (node == null)
{
this.TreeView1.Nodes.Add(no);
}
else
{
node.ChildNodes.Add(no);
}
}
}
private DataTable GetTreeData()
{
string strcnn = ConfigurationManager.ConnectionStrings["treeCon"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(strcnn))
{
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "select * from MenuTree order by parentid,menuorder";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
}
}
}