弹出式选择文本控件

很久没根新blog了,这段时间写了几个控件,整理下发出来,大家共享

  1 /*
  2  * 组件名称:    PopText
  3  * 功能说明:    弹出式选择文本控件,返回数据根据 '◇|◇' 分隔符解析
  4  * 作者:        Chating Yu
  5  * 时间:        2005-12-29 21:32
  6  * 版本修订:    V1.0 实现控件基本功能. 
  7  */ 
  8 using System;
  9 using System.Web;
 10 using System.Web.UI;
 11 using System.Web.UI.WebControls;
 12 
 13 namespace Cty.UserControls
 14 {
 15     /// <summary>
 16     /// 弹出式选择文本控件
 17     /// </summary>
 18     [ToolboxData("<{0}:PopText runat=\"server\"></{0}:PopText>")]
 19     public class PopText : UserControl, INamingContainer
 20     {
 21         private TextBox txtBox = new TextBox();
 22         private TextBox valueBox = new TextBox();
 23 
 24         /// <summary>
 25         /// 控件对外可见文本
 26         /// </summary>
 27         public string Text{
 28             get{
 29                 EnsureChildControls();
 30                 return txtBox.Text;
 31             }set{
 32                  txtBox.Text = value;
 33              }
 34         }
 35 
 36         /// <summary>
 37         /// 控件对外数据值
 38         /// </summary>
 39         public string Value{
 40             get{
 41                 EnsureChildControls();
 42                 return valueBox.Text;
 43             }set{
 44                  valueBox.Text = value;
 45              }
 46         }
 47 
 48         private string popUrl = "";
 49 
 50         /// <summary>
 51         /// 弹出数据选择网页
 52         /// </summary>
 53         public string PopUrl{
 54             get{
 55                 EnsureChildControls();
 56                 return popUrl;
 57             }set{
 58                  popUrl = value;
 59              }
 60         }
 61 
 62         /// <summary>
 63         /// 获取或设置由 Web 服务器控件在客户端呈现的级联样式表 (CSS) 类。
 64         /// </summary>
 65         public string CssClass{
 66             get{
 67                 EnsureChildControls();
 68                 return txtBox.CssClass;
 69             }set{
 70                  txtBox.CssClass = value;
 71              }
 72         }
 73 
 74         private int width = 500;
 75         private int height = 400;
 76 
 77         /// <summary>
 78         /// 弹出窗口得宽度(单位:象素)
 79         /// </summary>
 80         public int Width{
 81             get{
 82                 EnsureChildControls();
 83                 return width;
 84             }set{
 85                  width = value;
 86              }
 87         }
 88 
 89         /// <summary>
 90         /// 弹出窗口得高度(单位:象素)
 91         /// </summary>
 92         public int Height{
 93             get{
 94                 EnsureChildControls();
 95                 return height;
 96             }set{
 97                  height = value;
 98              }
 99         }
100 
101         /// <summary>
102         /// 创建包含的任何子控件,以便为回发或呈现做准备。
103         /// </summary>
104         protected override void CreateChildControls() {
105             txtBox.ID = "txtBox";
106             txtBox.ReadOnly = true;
107             Controls.Add(txtBox);
108 
109             valueBox.ID = "valueBox";
110             valueBox.ReadOnly = true;
111             Controls.Add(valueBox);
112             base.CreateChildControls ();
113         }
114 
115         /// <summary>
116         /// 将服务器控件内容发送到提供的 HtmlTextWriter 对象,此对象编写将在客户端呈现的内容。
117         /// </summary>
118         /// <param name="writer"></param>
119         protected override void Render(HtmlTextWriter writer) {
120             if(PopUrl == "")
121                 throw new Exception("没有指定弹出数据选着网页地址");
122             txtBox.Attributes.Add("onclick","PopSelectValue('"+txtBox.ClientID+"','"+valueBox.ClientID+"','"+PopUrl+"',"+Width.ToString()+","+Height.ToString()+")");
123             valueBox.Style.Add("display","none");
124 
125             if(!Page.IsClientScriptBlockRegistered("PopTextJs")){
126                 string clientJs = @"
127 <script language=""javascript"">
128 function PopSelectValue(tid,vid,u,width,height) {
129     //取得控件在页面中绝对位置
130     var otid = document.getElementById(tid);
131     var ovid = document.getElementById(vid);
132     var absLocation = GetAbsoluteLocation(otid) ;
133     if(absLocation == null)
134         return;
135     var abs = absLocation.split(',');
136     var top = window.screenTop+parseInt(abs[1])+otid.offsetHeight;
137     var left = window.screenLeft+parseInt(abs[0]);
138 
139     var v = showModalDialog(u,window,'dialogLeft='+left+';dialogTop='+top+';dialogWidth:'+width+'px; dialogHeight:'+height+'px;help:0;status:0;resizeable:1;');
140     //var v = showModalDialog(u,window,'dialogWidth:'+width+'px; dialogHeight:'+height+'px;help:0;status:0;resizeable:1;');
141     if(v == null) return;
142     var ret = v.split('◇|◇');
143     if(ret.length != 2) return;//格式不复合
144     //text
145     otid.value = ret[0];
146     //value
147     ovid.value = ret[1];
148 }
149 function GetAbsoluteLocation(element) 
150 
151     if ( element == null ) 
152     { 
153         return null; 
154     } 
155     var offsetTop = element.offsetTop; 
156     var offsetLeft = element.offsetLeft; 
157     while( element = element.offsetParent ) 
158     { 
159         offsetTop += element.offsetTop; 
160         offsetLeft += element.offsetLeft; 
161     } 
162     return offsetLeft+','+offsetTop ; 
163 }
164 
165 </script>
166 <!--  pop page return value like this
167 <script language='javascript'>
168 function document.onclick(){ // element event
169     window.parent.returnValue = '123◇|◇456';
170     window.parent.close();
171 }
172 </script>
173 -->";
174                 writer.Write(clientJs);
175                 this.Page.RegisterClientScriptBlock("PopTextJs","");
176             }
177             base.Render (writer);
178         }
179         
180     }
181 }
182 

效果图:


 
具体使用:
 
 1 <%@ Register TagPrefix="cc1" Namespace="Cty.UserControls" Assembly="Cty.UserControls" %>
 2 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Cty.CtyWeb.WebForm1" %>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 4 <HTML>
 5     <HEAD>
 6         <title>WebForm1</title>
 7         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
 8         <meta name="CODE_LANGUAGE" Content="C#">
 9         <meta name="vs_defaultClientScript" content="JavaScript">
10         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
11     </HEAD>
12     <body>
13         <form id="Form1" method="post" runat="server">
14             <P>
15                 <cc1:PopText PopUrl="Default.aspx" Width="100" Height="80" id="Poptext1" runat="server"></cc1:PopText></P>
16             <P>
17                 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
18         </form>
19     </body>
20 </HTML>
21 

1 private void Button1_Click(object sender, System.EventArgs e) {
2             Response.Write(Poptext1.Value);
3         }

   http://chating.cnblogs.com/archive/2005/12/29/307698.html
posted @ 2006-01-23 23:35  torome  阅读(274)  评论(0编辑  收藏  举报