模态窗体选择文本控件

模态窗体选择文本控件

经常使用摸态窗体,总是需要重复编写javascript脚本.所以封装了这个控件,这个控件使用的是aspnet2.0的API.所以用在1.1上的需要自己修改代码.

控件代码
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingSystem.ComponentModel;
usingSystem.Text;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;

namespaceDotnetClubPortal.WebControls
{
/**////<summary>
///用户选择控件
///</summary>

[DefaultProperty("Text"),
ToolboxData(
"<{0}:SelectObjectrunat=server></{0}:SelectObject>")]
publicclassSelectObject:,INamingContainer
{
protectedSystem.Web.UI.HtmlControls.HtmlInputHiddenhiddenObjectId;
protectedSystem.Web.UI.WebControls.TextBoxtxtObjectName;
protectedSystem.Web.UI.WebControls.Buttonbutton;


privateHtmlTableuiTable;

publicSelectObject()
{
txtObjectName
=newTextbox();
hiddenObjectId
=newHtmlInputHidden();
}


/**////<summary>
///ID,ID中间使用","分隔
///</summary>

[Browsable(false),Category("设置"),Description("取得的对象值,中间使用','分隔"),DefaultValue("")]
publicstringObjectId
{
get
{
returnhiddenObjectId.Value;
}

set
{
hiddenObjectId.Value
=value.ToString();
}

}


/**////<summary>
///名称,中间使用","分隔
///</summary>

[Browsable(false),Category("设置"),Description("对象名称,中间使用','分隔"),DefaultValue("")]
publicstringObjectName
{
get
{
returntxtObjectName.Text;
}

set
{
this.txtObjectName.Text=value.ToString();
}

}


[Category(
"设置"),Description("选择页面路径"),Browsable(true),NotifyParentProperty(true)]
publicstringPageUrl
{
get
{
objectobj=ViewState["PageUrl"];
return(obj==null)?string.Empty:obj.ToString();
}

set
{
ViewState[
"PageUrl"]=value;
}

}


/**////<summary>
///编辑框是否可见
///</summary>

[Category("设置"),Description("编辑框是否可见"),Browsable(true),DefaultValue(true)]
publicboolIsVisable
{
get
{
objectobj=ViewState["Visable"];
return(obj==null)?false:bool.Parse(obj.ToString());
}

set
{
ViewState[
"Visable"]=value;
}

}


protectedoverridevoidRender(HtmlTextWriteroutput)
{
base.Render(output);
Control[]CtrlArray;
CtrlArray
=newControl[3];
CtrlArray[
0]=this.hiddenObjectId;
CtrlArray[
1]=this.txtObjectName;
CtrlArray[
2]=this.button;
CreateTabeRow(CtrlArray);
}




/**////<summary>
///创建行
///</summary>
///<paramname="Obj">行中包含的控件</param>

privatevoidCreateTabeRow(Control[]Obj)
{
HtmlTableRowtr
=newHtmlTableRow();
HtmlTableCelltd
=newHtmlTableCell();
foreach(ControlcinObj)
{
if(c!=null)
{
td.Controls.Add(c);
}


}

tr.Cells.Add(td);
uiTable.Rows.Add(tr);
}


protectedoverridevoidOnPreRender(EventArgse)
{
RegisterClientScriptBlockJavaScript();
base.OnPreRender(e);
}


发出客户端脚本RegisterClientScriptBlockJavaScript()#region发出客户端脚本RegisterClientScriptBlockJavaScript()
privatevoidRegisterClientScriptBlockJavaScript()
{
//如果已经注册了脚本则不再注册
if(Page.ClientScript.IsClientScriptBlockRegistered("SetValue"))
{
return;
}

else
{
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),"SetValue",SetValue().ToString());
}


}


publicStringBuilderSetValue()
{
StringBuilderbuilder
=newStringBuilder();
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");
returnbuilder;

}


#endregion


protectedoverridevoidCreateChildControls()
{
uiTable
=newHtmlTable();
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=newButton();
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

你可能感兴趣的:(JavaScript,c,UI,Web,脚本)