本文主要是介绍WF的一个ASPNET例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很多朋友总是问我如何在ASPNET中使用WF
其实我从来不关心前台用什么实现,不管理是B/S还是C/S
手写了一个ASPNET页与WF工作流例子,
这个例子很简单,例子本身并没有什么价值
希望这个例子能给一些朋友代来其他方面的收获
aspx页面代码
<%@PageLanguage="C#"%>
<scriptrunat="server">
//工作流引擎,使用静态对象,使所有用户都可共享,并在application生命周期存活
publicstaticSystem.Workflow.Runtime.WorkflowRuntimeruntime;
//启动引擎按钮
System.Web.UI.WebControls.ButtonbtStartRuntime=newSystem.Web.UI.WebControls.Button();
//得到实例列表按钮
System.Web.UI.WebControls.ButtonbtListInstanct=newSystem.Web.UI.WebControls.Button();
//实例列表
System.Web.UI.WebControls.RadioButtonListrlList=newSystem.Web.UI.WebControls.RadioButtonList();
//创建实例按钮
System.Web.UI.WebControls.ButtonbtCreateInstance=newSystem.Web.UI.WebControls.Button();
//下一步按钮
System.Web.UI.WebControls.ButtonbtNext=newSystem.Web.UI.WebControls.Button();
protectedvoidPage_Load(objectsender,EventArgse)
{
if(runtime==null)
{
System.Web.HttpContext.Current.Response.Write("引擎没有启动<p>");
}
else
{
System.Web.HttpContext.Current.Response.Write("引擎已启动<p>");
}
//
btStartRuntime.Text="启动/重新启动引擎";
btStartRuntime.Click+=newEventHandler(btStartRuntime_Click);
form1.Controls.Add(btStartRuntime);
//
btCreateInstance.Text="创建实例";
form1.Controls.Add(newSystem.Web.UI.LiteralControl("<p>"));
btCreateInstance.Click+=newEventHandler(btCreateInstance_Click);
form1.Controls.Add(btCreateInstance);
//
form1.Controls.Add(newSystem.Web.UI.LiteralControl("<p>"));
btListInstanct.Text="得到实例列表";
btListInstanct.Click+=newEventHandler(btListInstanct_Click);
form1.Controls.Add(btListInstanct);
//
form1.Controls.Add(newSystem.Web.UI.LiteralControl("<p>"));
form1.Controls.Add(rlList);
//
form1.Controls.Add(newSystem.Web.UI.LiteralControl("<p>"));
btNext.Text="下一步";
btNext.Click+=newEventHandler(btNext_Click);
form1.Controls.Add(btNext);
}
voidbtNext_Click(objectsender,EventArgse)
{
if(rlList.SelectedItem!=null)
{
System.Workflow.Runtime.WorkflowInstancetemp=runtime.GetWorkflow(newGuid(rlList.SelectedItem.Text));
temp.Resume();
System.Threading.Thread.Sleep(1000);
}
}
voidbtCreateInstance_Click(objectsender,EventArgse)
{
runtime.CreateWorkflow(typeof(自定义工作流)).Start();
}
voidbtListInstanct_Click(objectsender,EventArgse)
{
rlList.Items.Clear();
foreach(System.Workflow.Runtime.WorkflowInstancetempinruntime.GetLoadedWorkflows())
{
System.Web.UI.WebControls.ListItemli=newListItem(temp.InstanceId.ToString());
rlList.Items.Add(li);
}
}
voidbtStartRuntime_Click(objectsender,EventArgse)
{
runtime=newSystem.Workflow.Runtime.WorkflowRuntime();
runtime.Started+=newEventHandler<System.Workflow.Runtime.WorkflowRuntimeEventArgs>(runtime_Started);
runtime.WorkflowCreated+=newEventHandler<System.Workflow.Runtime.WorkflowEventArgs>(runtime_WorkflowCreated);
runtime.StartRuntime();
}
voidruntime_WorkflowCreated(objectsender,System.Workflow.Runtime.WorkflowEventArgse)
{
System.Web.HttpContext.Current.Response.Write(e.WorkflowInstance.InstanceId.ToString()+"实例建立<p>");
}
voidruntime_Started(objectsender,System.Workflow.Runtime.WorkflowRuntimeEventArgse)
{
System.Web.HttpContext.Current.Response.Write("引擎启动成功<p>");
}
class自定义工作流:System.Workflow.Activities.SequentialWorkflowActivity
{
/*
[code1(CodeActivity)]
↓
[su1(SuspendActivity)]
↓
[code2(CodeActivity)]
↓
[su1(SuspendActivity)]
↓
[code3(CodeActivity)]
*/
public自定义工作流()
{
//
System.Workflow.Activities.CodeActivitycode1=newSystem.Workflow.Activities.CodeActivity("code1");
code1.ExecuteCode+=delegate(objects,EventArgsee){};
//
System.Workflow.ComponentModel.SuspendActivitysu1=newSystem.Workflow.ComponentModel.SuspendActivity("su1");
//
System.Workflow.Activities.CodeActivitycode2=newSystem.Workflow.Activities.CodeActivity("code2");
code2.ExecuteCode+=delegate(objects,EventArgsee){};
//
System.Workflow.ComponentModel.SuspendActivitysu2=newSystem.Workflow.ComponentModel.SuspendActivity("su2");
//
System.Workflow.Activities.CodeActivitycode3=newSystem.Workflow.Activities.CodeActivity("code3");
code3.ExecuteCode+=delegate(objects,EventArgsee){};
//
this.CanModifyActivities=true;
this.Activities.Add(code1);
this.Activities.Add(su1);
this.Activities.Add(code2);
this.Activities.Add(su2);
this.Activities.Add(code3);
this.CanModifyActivities=false;
}
}
</script>
<html>
<body>
<formid="form1"runat="server">
</form>
</body>
</html>
Web.Config文件
<?xmlversion="1.0"?>
<configuration>
<system.web>
<compilation>
<assemblies>
<addassembly="System.Workflow.Activities,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
<addassembly="System.Workflow.ComponentModel,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
<addassembly="System.Workflow.Runtime,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
</configuration>
保存一下,注意编辑格式,放到IIS里的就可以运行了
-----------------------------------------------------------------------
这篇关于WF的一个ASPNET例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!