弹出式模态窗体选择文本控件
2006年就要到来了,最近比较忙,很少更新blog,今天发一个模态窗体选择文本控件辞旧迎新.新年在发几个asp.net2.0 webPart控件同各位分享:
经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.
2006年就要到来了,最近比较忙,很少更新blog,今天发一个模态窗体选择文本控件辞旧迎新.新年在发几个asp.net2.0 webPart控件同各位分享:
经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.
具体使用 :
<cc1:SelectObject ID="PowerFlag" IsVisable="true" PageUrl="../../../../CommonPage/GetGroupInfo.aspx" runat="server">
</cc1:SelectObject>
通过属性ObjectName,ObjectId获取选择返回的值.
经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.
控件代码
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}:SelectObject runat=server></{0}:SelectObject>")]
public class SelectObject: ,INamingContainer
{
protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenObjectId;
protected System.Web.UI.WebControls.TextBox txtObjectName;
protected System.Web.UI.WebControls.Button button;
private HtmlTable uiTable;
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(HtmlTextWriter output)
{
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>
/// <param name="Obj">行中包含的控件</param>
private void CreateTabeRow(Control [] Obj)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
foreach (Control c in Obj)
{
if (c != null)
{
td.Controls.Add(c);
}
}
tr.Cells.Add(td);
uiTable.Rows.Add(tr);
}
protected override void OnPreRender(EventArgs e)
{
RegisterClientScriptBlockJavaScript();
base.OnPreRender (e);
}
发出客户端脚本 RegisterClientScriptBlockJavaScript()#region 发出客户端脚本 RegisterClientScriptBlockJavaScript()
private void RegisterClientScriptBlockJavaScript()
{
//如果已经注册了脚本则不再注册
if(Page.ClientScript.IsClientScriptBlockRegistered("SetValue"))
{
return;
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SetValue",SetValue().ToString());
}
}
public StringBuilder SetValue()
{
StringBuilder builder=new StringBuilder();
builder.Append("<script language=\"javascript\">\n");
builder.Append(" function SetValue(Url,Name,Id){\n");
builder.Append(" var vDialog;\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 = new TextBox();
txtObjectName.ID = "ObjectName";
txtObjectName.Visible = this.IsVisable;
//hiddenObjectId= new HtmlInputHidden();
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);
}
}
}
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}:SelectObject runat=server></{0}:SelectObject>")]
public class SelectObject: ,INamingContainer
{
protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenObjectId;
protected System.Web.UI.WebControls.TextBox txtObjectName;
protected System.Web.UI.WebControls.Button button;
private HtmlTable uiTable;
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(HtmlTextWriter output)
{
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>
/// <param name="Obj">行中包含的控件</param>
private void CreateTabeRow(Control [] Obj)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
foreach (Control c in Obj)
{
if (c != null)
{
td.Controls.Add(c);
}
}
tr.Cells.Add(td);
uiTable.Rows.Add(tr);
}
protected override void OnPreRender(EventArgs e)
{
RegisterClientScriptBlockJavaScript();
base.OnPreRender (e);
}
发出客户端脚本 RegisterClientScriptBlockJavaScript()#region 发出客户端脚本 RegisterClientScriptBlockJavaScript()
private void RegisterClientScriptBlockJavaScript()
{
//如果已经注册了脚本则不再注册
if(Page.ClientScript.IsClientScriptBlockRegistered("SetValue"))
{
return;
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SetValue",SetValue().ToString());
}
}
public StringBuilder SetValue()
{
StringBuilder builder=new StringBuilder();
builder.Append("<script language=\"javascript\">\n");
builder.Append(" function SetValue(Url,Name,Id){\n");
builder.Append(" var vDialog;\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 = new TextBox();
txtObjectName.ID = "ObjectName";
txtObjectName.Visible = this.IsVisable;
//hiddenObjectId= new HtmlInputHidden();
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获取选择返回的值.
欢迎大家扫描下面二维码成为我的客户,扶你上云