代码改变世界

.Net数据源自定义参数

2007-10-27 19:43  sun@live  阅读(240)  评论(0编辑  收藏  举报
1.web控件源码
namespace ControlLibrary {
    
using System;
    
using System.Data;
    
using System.Configuration;
    
using System.Web;
    
using System.Web.Security;
    
using System.Web.UI;
    
using System.Web.UI.WebControls;
    
using System.Web.UI.WebControls.WebParts;
    
using System.Web.UI.HtmlControls;


    
public class PagePropertyParameter : Parameter {

        
private String _key;

        
public String Key {
            
get {
                
return _key;
            }

            
set {
                _key 
= value;
            }

        }


        
protected override object Evaluate(HttpContext context, Control control) {
            Type t 
= control.Page.GetType();
            System.Reflection.PropertyInfo pi 
= t.GetProperty(Key, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
            
return pi.GetValue(control.Page, null);
        }

    }


    
public class ViewStateParameter : Parameter {

        
private String _key;

        
public String Key {
            
get {
                
return _key;
            }

            
set {
                _key 
= value;
            }

        }


        
protected override object Evaluate(HttpContext context, Control control) {
            Type t 
= control.Page.GetType();
            System.Reflection.PropertyInfo pi 
= t.GetProperty("ViewState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            StateBag pageViewState 
= pi.GetValue(control.Page, nullas StateBag;
            
return pageViewState[Key];
        }

    }


    
public class RequestParameter : Parameter
    
{
        
private String _key;

        
public String Key
        
{
            
get
            
{
                
return _key;
            }

            
set
            
{
                _key 
= value;
            }

        }


        
protected override object Evaluate(HttpContext context, Control control)
        
{
            
return context.Request.Params.Get(_key);
           
// return base.Evaluate(context, control);
        }

    }

}
2.使用方法
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:pubsConnectionString1.ProviderName %>" SelectCommand="SELECT [stor_id], [stor_name], [stor_address], [city], [state], [zip] FROM [stores] WHERE ([stor_name] LIKE '%' + @stor_name + '%')">
            <SelectParameters>
                <%--<cc1:ViewStateParameter Name="stor_name" Key="TestState" Type="String" />--%>
               <%-- <cc1:PagePropertyParameter Name="stor_name" Key="TestValue" Type="String" />--%>
                <cc1:RequestParameter Name="stor_name" Key="Name" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>