webControl 中使用资源 与嵌入资源

来源:互联网 发布:bring with it的it 编辑:程序博客网 时间:2024/05/10 03:46

不废话了.打开VS2005 新建一个解决方案 驗證控件

1 新建一个类库

新建一个C#类库  

2 把class.cs改名为 "使用资源读取,输出等.cs"

3,添加资源 

 3.1 选择 "属性"

添加資源

 

 

 

添加

添加完成後如下圖

添加完成後

在 輸入代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Resources;
using System.Web.UI.WebControls;
[assembly: WebResource(
"FormValidation.Validation.xml""text/xml")]
[assembly: WebResource(
"FormValidation.Sunset.jpg""Image/jpg")]
/*
 * [assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")] 
 *     我们可以看msdn里有WebResource的参数说明:第一个是资源的名字,第二个是资源的mime-type名。 
 该技术是通过在控件代码里调用GetWebResourceUrl方法,这个方法指向一个名为WebResource.axd的内置HTTP处理程序的URL。通过加载一个名为AssemblyResourceLoader的HttpHandler类,ASP.NET运行时响应WebResource.axd的请求,返回指定资源的URL。
然后在程序中调用如下: 
 * m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg"); 
 
*/

namespace FormValidation
{

    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:FormValidationVer3 runat=server></{0}:FormValidationVer3>")]
    
public class FormValidation : WebControl
    
{
        
// private   System.Resources.ResourceManager   rm;  //新建一个资源管理
        
        [Bindable(
true)]
        [Category(
"Appearance")]
        [DefaultValue(
"")]
        [Localizable(
true)]
        
public string Text
        
{
            
get
            
{
                String s 
= (String)ViewState["Text"];
                
return ((s == null? String.Empty : s);
            }


            
set
            
{
                ViewState[
"Text"= value;
            }

        }


        
protected override void RenderContents(HtmlTextWriter output)
        
{

            ResourceManager R资源管理 
= new ResourceManager("驗證控件.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
            String script 
= R资源管理.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, truetrue).GetString("Validation");//得到字符串
            output.Write(script);
           
// System.Drawing.Image img = R资源管理.GetObject("Sunset");
            string ss = R资源管理.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, truetrue).GetString("HTMLPage1");
            
////////////////
           string xmlurl = Page.ClientScript.GetWebResourceUrl(GetType(), "Validation.xml");
            
//output.Write(R资源管理.GetObject("FormValidation.Validation.xml")); 
            string url = Page.ClientScript.GetWebResourceUrl(GetType(), "Sunset.jpg");
            output.Write(
"<img src="" + url+"" />");
            output.Write(xmlurl);
//动态生成WebResource
            output.Write(ss);//输出文本文件
            output.Write(Text);

        }




        
/// <summary>
        
/// 重写 OnPreRender 方法。 输出一回 脚本 东东
        
/// </summary>
        
/// <param name="e">包含事件数据的 EventArgs 对象。</param>

        protected override void OnPreRender(EventArgs e)//注册脚本的操作
        {
            
string str = "<script language="javascript"  type="text/javascript">function bb(){alert('我是AA如果注册了 只会出一个AA 不是就出错哒 "+Page.Form.Name+"');} </script>";
            ClientScriptManager aa 
= Page.ClientScript;
            
if (!aa.IsClientScriptBlockRegistered("aa"))//看看这个关键字的脚本是不是已经注册了
            {
                aa.RegisterClientScriptBlock(
this.GetType(), "aa", str);//最简单的一种注册方式
            }

            
            
        }






    }

}

原创粉丝点击