弹出式模态窗体选择文本控件

博主为辞旧迎新发布模态窗体选择文本控件,并将在新年分享asp.net2.0 webPart控件。因常需重复编写javascript脚本,博主封装了该控件,介绍了控件代码及具体使用方法,可通过属性ObjectName、ObjectId获取选择返回的值。
2006年就要到来了,最近比较忙,很少更新blog,今天发一个模态窗体选择文本控件辞旧迎新.新年在发几个asp.net2.0 webPart控件同各位分享:
经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.

ContractedBlock.gifExpandedBlockStart.gif控件代码
None.gifusing System;
None.gif
using System.ComponentModel;
None.gif
using System.Text;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Web.UI.WebControls;
None.gif
None.gif
namespace DotnetClubPortal.WebControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 用户选择控件
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Text"), 
InBlock.gif    ToolboxData(
"<{0}:SelectObject runat=server></{0}:SelectObject>")]
InBlock.gif    
public class SelectObject: ,INamingContainer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenObjectId;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txtObjectName;
InBlock.gif        
protected System.Web.UI.WebControls.Button button;
InBlock.gif
InBlock.gif
InBlock.gif        
private HtmlTable uiTable;
InBlock.gif
InBlock.gif        
public SelectObject()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            txtObjectName 
= new Textbox();
InBlock.gif          hiddenObjectId
= new HtmlInputHidden();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// ID,ID中间使用","分隔
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Browsable(false),Category("设置"),Description("取得的对象值,中间使用','分隔"),DefaultValue("")]
InBlock.gif        
public string ObjectId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return hiddenObjectId.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hiddenObjectId.Value
=value.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 名称,中间使用","分隔
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Browsable(false),Category("设置"),Description("对象名称,中间使用','分隔"),DefaultValue("")]
InBlock.gif        
public string ObjectName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return txtObjectName.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.txtObjectName.Text = value.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Category(
"设置"),Description("选择页面路径"),Browsable(true),    NotifyParentProperty(true)]
InBlock.gif        
public string PageUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["PageUrl"];
InBlock.gif                
return (obj==null)?string.Empty:obj.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"PageUrl"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 编辑框是否可见
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Category("设置"),Description("编辑框是否可见"),Browsable(true),DefaultValue(true)]
InBlock.gif        
public bool IsVisable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object obj=ViewState["Visable"];
InBlock.gif                
return (obj==null)?false:bool.Parse(obj.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"Visable"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.Render(output);
InBlock.gif            Control [] CtrlArray;
InBlock.gif            CtrlArray 
= new Control [ 3];
InBlock.gif            CtrlArray[
0= this.hiddenObjectId;
InBlock.gif            CtrlArray[
1= this.txtObjectName;
InBlock.gif            CtrlArray[
2= this.button;
InBlock.gif            CreateTabeRow(CtrlArray);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建行
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="Obj">行中包含的控件</param>

InBlock.gif        private void CreateTabeRow(Control [] Obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HtmlTableRow tr 
= new HtmlTableRow();
InBlock.gif            HtmlTableCell td 
= new HtmlTableCell();
InBlock.gif            
foreach (Control c in Obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (c != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    td.Controls.Add(c);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            tr.Cells.Add(td);
InBlock.gif            uiTable.Rows.Add(tr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RegisterClientScriptBlockJavaScript();
InBlock.gif            
base.OnPreRender (e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
发出客户端脚本 RegisterClientScriptBlockJavaScript()#region 发出客户端脚本 RegisterClientScriptBlockJavaScript()
InBlock.gif        
private void RegisterClientScriptBlockJavaScript()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//如果已经注册了脚本则不再注册
InBlock.gif
            if(Page.ClientScript.IsClientScriptBlockRegistered("SetValue"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "SetValue",SetValue().ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public StringBuilder SetValue()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder builder
=new StringBuilder();
InBlock.gif            builder.Append(
"<script language=\"javascript\">\n");
InBlock.gif            builder.Append(
"  function SetValue(Url,Name,Id){\n");
InBlock.gif            builder.Append(
"      var vDialog;\n");
InBlock.gif            builder.Append(
"      vDialog = window.showModalDialog(Url ,window,\"dialogHeight:500px;dialogWidth:320px;status:no;resizable:yes;scroll:no;\");\n");
InBlock.gif            builder.Append(
"      if (vDialog != null){");
InBlock.gif            builder.Append(
"if(document.getElementById(Name))document.getElementById(Name).value  = vDialog.oSelectName;\n");
InBlock.gif            builder.Append(
"if(document.getElementById(Id))document.getElementById(Id).value  = vDialog.oSelectNameID;\n");
InBlock.gif            builder.Append(
"}}</script>\n");
InBlock.gif            
return builder;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            uiTable 
= new HtmlTable();
InBlock.gif            uiTable.ID 
= "uiTable";
InBlock.gif            uiTable.Width 
= "100%";
InBlock.gif            uiTable.Border 
= 0;
InBlock.gif
InBlock.gif            
//txtObjectName = new TextBox();
InBlock.gif
            txtObjectName.ID = "ObjectName";
InBlock.gif            txtObjectName.Visible 
= this.IsVisable;
InBlock.gif            
InBlock.gif            
//hiddenObjectId= new HtmlInputHidden();
InBlock.gif
            hiddenObjectId.ID = "ObjectId";
InBlock.gif
InBlock.gif            
this.button = new Button();
InBlock.gif            
this.button.ID = "btnSelect";
InBlock.gif            
this.button.Attributes.Add("width","100");
InBlock.gif            
this.button.Text = "选择";
InBlock.gif            
this.button.Attributes.Add("onclick",string.Format("javascript:SetValue('{0}','{1}','{2}');",PageUrl,this.ClientID +"_ObjectName",this.ClientID+"_ObjectId"));
InBlock.gif            
this.button.CausesValidation = false;
InBlock.gif            
InBlock.gif            
this.Controls.Add(uiTable);
InBlock.gif            
this.Controls.Add(hiddenObjectId);
InBlock.gif            
this.Controls.Add(txtObjectName);
InBlock.gif            
this.Controls.Add(button);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

具体使用 :

            <cc1:SelectObject ID="PowerFlag" IsVisable="true" PageUrl="../../../../CommonPage/GetGroupInfo.aspx" runat="server">
            </cc1:SelectObject>
通过属性ObjectName,ObjectId获取选择返回的值.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值