模态窗体选择文本控件
经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace DotnetClubPortal.WebControls
{
/// <summary>
/// 用户选择控件
/// </summary>
[DefaultProperty( " Text " ),
ToolboxData( " <{0}:SelectObjectrunat=server></{0}:SelectObject> " )]
public class SelectObject:,INamingContainer
{
protected System.Web.UI.HtmlControls.HtmlInputHiddenhiddenObjectId;
protected System.Web.UI.WebControls.TextBoxtxtObjectName;
protected System.Web.UI.WebControls.Buttonbutton;
private HtmlTableuiTable;
public SelectObject()
{
txtObjectName = new Textbox();
hiddenObjectId = new HtmlInputHidden();
}
/// <summary>
/// ID,ID中间使用","分隔
/// </summary>
[Browsable( false ),Category( " 设置 " ),Description( " 取得的对象值,中间使用','分隔 " ),DefaultValue( "" )]
public string ObjectId
{
get
{
return hiddenObjectId.Value;
}
set
{
hiddenObjectId.Value = value.ToString();
}
}
/// <summary>
/// 名称,中间使用","分隔
/// </summary>
[Browsable( false ),Category( " 设置 " ),Description( " 对象名称,中间使用','分隔 " ),DefaultValue( "" )]
public string ObjectName
{
get
{
return txtObjectName.Text;
}
set
{
this .txtObjectName.Text = value.ToString();
}
}
[Category( " 设置 " ),Description( " 选择页面路径 " ),Browsable( true ),NotifyParentProperty( true )]
public string PageUrl
{
get
{
object obj = ViewState[ " PageUrl " ];
return (obj == null ) ? string .Empty:obj.ToString();
}
set
{
ViewState[ " PageUrl " ] = value;
}
}
/// <summary>
/// 编辑框是否可见
/// </summary>
[Category( " 设置 " ),Description( " 编辑框是否可见 " ),Browsable( true ),DefaultValue( true )]
public bool IsVisable
{
get
{
object obj = ViewState[ " Visable " ];
return (obj == null ) ? false : bool .Parse(obj.ToString());
}
set
{
ViewState[ " Visable " ] = value;
}
}
protected override void Render(HtmlTextWriteroutput)
{
base .Render(output);
Control[]CtrlArray;
CtrlArray = new Control[ 3 ];
CtrlArray[ 0 ] = this .hiddenObjectId;
CtrlArray[ 1 ] = this .txtObjectName;
CtrlArray[ 2 ] = this .button;
CreateTabeRow(CtrlArray);
}
/// <summary>
/// 创建行
/// </summary>
/// <paramname="Obj"> 行中包含的控件 </param>
private void CreateTabeRow(Control[]Obj)
{
HtmlTableRowtr = new HtmlTableRow();
HtmlTableCelltd = new HtmlTableCell();
foreach (Controlc in Obj)
{
if (c != null )
{
td.Controls.Add(c);
}
}
tr.Cells.Add(td);
uiTable.Rows.Add(tr);
}
protected override void OnPreRender(EventArgse)
{
RegisterClientScriptBlockJavaScript();
base .OnPreRender(e);
}
#region 发出客户端脚本RegisterClientScriptBlockJavaScript()
private void RegisterClientScriptBlockJavaScript()
{
// 如果已经注册了脚本则不再注册
if (Page.ClientScript.IsClientScriptBlockRegistered( " SetValue " ))
{
return ;
}
else
{
Page.ClientScript.RegisterClientScriptBlock( this .GetType(), " SetValue " ,SetValue().ToString());
}
}
public StringBuilderSetValue()
{
StringBuilderbuilder = new StringBuilder();
builder.Append( " <scriptlanguage=\ " javascript\ " >\n " );
builder.Append( " functionSetValue(Url,Name,Id){\n " );
builder.Append( " varvDialog;\n " );
builder.Append( " vDialog=window.showModalDialog(Url,window,\ " dialogHeight:500px;dialogWidth:320px;status:no;resizable:yes;scroll:no;\ " );\n " );
builder.Append( " if(vDialog!=null){ " );
builder.Append( " if(document.getElementById(Name))document.getElementById(Name).value=vDialog.oSelectName;\n " );
builder.Append( " if(document.getElementById(Id))document.getElementById(Id).value=vDialog.oSelectNameID;\n " );
builder.Append( " }}</script>\n " );
return builder;
}
#endregion
protected override void CreateChildControls()
{
uiTable = new HtmlTable();
uiTable.ID = " uiTable " ;
uiTable.Width = " 100% " ;
uiTable.Border = 0 ;
// txtObjectName=newTextBox();
txtObjectName.ID = " ObjectName " ;
txtObjectName.Visible = this .IsVisable;
// hiddenObjectId=newHtmlInputHidden();
hiddenObjectId.ID = " ObjectId " ;
this .button = new Button();
this .button.ID = " btnSelect " ;
this .button.Attributes.Add( " width " , " 100 " );
this .button.Text = " 选择 " ;
this .button.Attributes.Add( " onclick " , string .Format( " javascript:SetValue('{0}','{1}','{2}'); " ,PageUrl, this .ClientID + " _ObjectName " , this .ClientID + " _ObjectId " ));
this .button.CausesValidation = false ;
this .Controls.Add(uiTable);
this .Controls.Add(hiddenObjectId);
this .Controls.Add(txtObjectName);
this .Controls.Add(button);
}
}
}
具体使用 :
<cc1:SelectObject ID="PowerFlag" IsVisable="true" PageUrl="../../../../CommonPage/GetGroupInfo.aspx" runat="server">
</cc1:SelectObject>
通过属性ObjectName,ObjectId获取选择返回的值.
原文: http://shanyou.cnblogs.com/archive/2005/12/31/309171.html