Quick Start中创建可以传递参数的web用户自定义控件

例1:http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/pagelets/Pagelet3.src&file=CS\Pagelet3.ascx&font=3
<%@ Register TagPrefix="Acme" TagName="Address" Src="pagelet3.ascx" %>

<html>

    
<script language="C#" runat="server">

        
void SubmitBtn_Click(Object sender, EventArgs E) {
        
            MyLabel.Text 
+= "<b>Shipping Address:</b> "
                         
+   ShipAddr.Address + "" 
                         
+   ShipAddr.City + "" 
                         
+   ShipAddr.State + "" 
                         
+   ShipAddr.Zip + "<br>";

            MyLabel.Text 
+= "<b>Billing Address:</b> "
                         
+   BillAddr.Address + "" 
                         
+   BillAddr.City + "" 
                         
+   BillAddr.State + "" 
                         
+   BillAddr.Zip + "<br>";
        }


    
</script>

<body style="font: 10pt verdana">

  
<h3>A Simple User Control w/ Properties</h3>

  
<form runat="server">

    
<Acme:Address id="ShipAddr" Caption="Shipping Address" Address="One Microsoft Way" City="Redmond" State="WA" Zip="98052" runat="server"/>

    
<p>

    
<Acme:Address id="BillAddr" Caption="Billing Address" runat="server"/>

    
<p>

    
<asp:button Text="Submit Form" OnClick="SubmitBtn_Click" runat=server/>

  
</form>

  
<asp:Label id="MyLabel" runat="server"/>

</body>
</html>


<script language="C#" runat="server">

  
public String Caption = "Address";

  
public String Address {
    
get {
      
return TxtAddress.Value;
    }

    
set {
      TxtAddress.Value 
= value;
    }

  }


  
public String City {
    
get {
      
return TxtCity.Value;
    }

    
set {
      TxtCity.Value 
= value;
    }

  }


  
public String State {
    
get {
      
return TxtState.Value;
    }

    
set {
      TxtState.Value 
= value;
    }

  }


  
public String Zip {
    
get {
      
return TxtZip.Value;
    }

    
set {
      TxtZip.Value 
= value;
    }

  }


</script>

<p>
<table style="font: 10pt verdana">
  
<tr>
    
<td colspan="6" style="padding-bottom:10">
      
<b><%=Caption%></b>
    
</td>
  
</tr>
  
<tr>
    
<td>
      Address: 
    
</td>
    
<td colspan="5">
      
<input id="TxtAddress" size="50" type="text" runat="server">
    
</td>
  
</tr>
  
<tr>
    
<td>
      City: 
    
</td>
    
<td>
      
<input id="TxtCity" type="text" runat="server">
    
</td>
    
<td>
      State: 
    
</td>
    
<td>
      
<input id="TxtState" size="2" type="text" runat="server">
    
</td>
    
<td>
      Zip: 
    
</td>
    
<td>
      
<input id="TxtZip" size="5" type="text" runat="server">
    
</td>
  
</tr>
</table>

例2:http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/simple/SimpleProperty.src&file=CS\SimpleProperty.aspx&font=3
<%@ Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" Assembly="SimpleControlSamples" %>

<html>

   
<body>

      
<form method="POST" runat="server">

          
<SimpleControlSamples:SimpleSubProperty Message="Hello There" Format-Color="red" Format-Size="3" runat=server/>

      
</form>

   
</body>

</html>


//-----------------------------------------------------------------------
//  This file is part of the Microsoft .NET SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;

namespace SimpleControlSamples {

    
public class Formater {
        
private int            pSize;
        
private System.String  pColor;

        
public Formater(System.Int32 size, System.String color) {
           
this.pSize = size;
           
this.pColor = color;
        }

        
public String Color {
            
get {
                
return pColor;
            }

            
set {
                pColor 
= value;
            }

        }

        
public int Size {
            
get {
                
return pSize;
            }

            
set {
                pSize 
= value;
            }

        }

    }


    
public class SimpleSubProperty : Control {

       
private Formater _format  = new Formater(3"black");
       
private String   _message = null;

       
public String Message {
           
get {
              
return _message;
           }

           
set {
              _message 
= value;
           }

       }


       
public Formater Format {
           
get {
              
return _format;
           }

       }


       
protected override void Render(HtmlTextWriter output) {
           output.Write(
"<font size=" + Format.Size + " color=" + Format.Color + ">");
           output.Write(_message);
           output.Write(
"</font>");
       }

    }
    
}



posted on 2004-12-23 10:25  找不到服务器  阅读(446)  评论(0编辑  收藏  举报

导航