嵌入Windows User Control到ASP.NET web form

2008-07-13  来源:   浏览次数 5

实现步骤:

  1. 新建Windows User Control项目,加入自己想做的事情(一些控件、方法、属性)
  2. 新建或打开Web项目,在相应页面中加入
<object id="MyWinControl1" height="200" width="600" classid='http:WinControl1.dll#WinControl1.UserControl1' viewastext>
</object>

 

编译项目,并把user control的dll文件拷贝到web的根目录下,简单的便可以执行了。

为了从网页传递一些初始值给UserControl的属性,在对象中加入param

<object id="MyWinControl1" height="200" width="600" classid='http:WinControl1.dll#WinControl1.UserControl1' viewastext>
  
<PARAM NAME='SomeText' VALUE='test'> </OBJECT> <!-- Property named "SomeText" in WinControl1.UserControl1 -->
</object>

为了每次修改过控件后网页中还可以正常显示,新建一个console项目去改名并拷贝

        static void Main(string[] args)
        
{
            
if (args.Length != 2)
                
return;

            
string fileName = args[0];
            
string outputPath = args[1];

            Debug.WriteLine(fileName);
            Debug.WriteLine(outputPath);

            
string version = Assembly.LoadFile(fileName).GetName().Version.ToString();
            outputPath 
= Path.Combine(outputPath, Path.GetFileName(fileName).Replace(".dll", version + ".dll"));

            File.Copy(fileName, outputPath, 
true);
        }

修改WindowsControl项目属性,在build events的post-build中加入:
"$(SolutionDir)Rename.exe" "$(TargetPath)" "$(SolutionDir)WinControlWeb\ "
这样便每次把项目以原有名称加版本号拷贝到web项目下,web页面code behind加入以下代码自动取得最新的dll和删掉旧的dll(web用的vb.net)

    Public Function GetFileName(ByVal controlPrefix As String)
        
'delete old, only get latest file
        Dim fs As List(Of String= New List(Of String)(Directory.GetFiles(Server.MapPath("~"), controlPrefix & "*.dll"))
        fs.Sort()
        
Dim lastIndex = fs.Count - 1
        
Dim result = Path.GetFileName(fs(lastIndex))
        fs.RemoveAt(lastIndex)
        
For Each s As String In fs
            File.Delete(s)
        
Next
        
Return result
    
End Function

web页面修改为

<object id="MyWinControl1" height="200" width="600" classid='http:<%= GetFileName("WinControl1") %>#WinControl1.UserControl1' viewastext>
  
<PARAM NAME='SomeText' VALUE='test'> </OBJECT> <!-- Property named "SomeText" in WinControl1.UserControl1 -->
</object>

但若是想用javascript取的控件的属性或调用控件的方法,需要将控件的ComVisible设为true(在Assembly.cs文件中)

[assembly: ComVisible(true)]

以上是直接使用visual studio调试,若部署后,客户端访问需要更改本地权限,需要新建一个代码组并赋予权限,执行如下命令:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -quiet -machine -addgroup All_Code -url http://localhost/* FullTrust -name "GroupName"
http://localhost/* 中的localhost换成实际网站或主机,指定一个GroupName
以上命令可以放到一个consle项目中去自动执行

        static void Main(string[] args)
        
{
            
try
            
{
                Process.Start(
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe",
                              
@"-quiet -machine -addgroup All_Code -url http://localhost/* FullTrust -name ""MyTest""");
                Console.WriteLine(
"设置成功!按任意键退出。");
                Console.Read();
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"发生异常,请将以下错误信息发送给管理员。");
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

            
        }

在页面中加入一个链接让用户自己去点击用来设定权限

<href="Setup.exe">Click here to download and run the setup program.</a>

当点击链接后直接点运行,出现问题,那就用rar做一个自解压包,解压后运行Setup.exe,问题解决。

上一篇:ASP.NET MVC Tip ..    下一篇:Flash图片轮换显..

相关主题:

网友评论