小菜一碟:让web页面部分内容只读
常常需要根据情况让某页面部分内容只读,我的实现思路是:把一些控件放在一个panel里,从codebehind里加入css style,然后css style用htc实现,保持初始化的内容。
程序如下:
//这是设置panel只读的函数,其中staffvac是web form项目的名字
static public void fcnSetPanelReadonly(System.Web.UI.WebControls.Panel pnlI, bool blReadOnly)
{
if(blReadOnly == false)
return;
int iCtrlCnt = pnlI.Controls.Count;
for(int i=0;i<iCtrlCnt;i++)
{
System.Web.UI.Control ctlI = pnlI.Controls[i];
try
{
System.Web.UI.WebControls.WebControl wctI = (System.Web.UI.WebControls.WebControl)ctlI;
wctI.Style.Add("behavior", "url(/StaffVac/script/Athos.DhtmlBehavior.ReadOnly.htc)");
}
catch(Exception exp)
{
string strErr = exp.Message;
}
}
}
//这是htc文件Athos.DhtmlBehavior.ReadOnly.htc,存放在当前web form项目的script目录下
<PUBLIC:COMPONENT URN="urn:msdn-microsoft-com:workshop" >
<public:attach event=oncontentready onevent="oncontentready()" />
<SCRIPT LANGUAGE="JScript">
function oncontentready()
{
strTagname = tagName;
strType = "";
if(strTagname == "INPUT")
{
strType = type;
if(strType == "text")
{
strValue = value;
element.attachEvent("onchange", fcnRestore);
}
else if(strType == "submit")
{
element.disabled = true;
}
}
else if(strTagname == "TEXTAREA")
{
strInnerHTML = String(innerHTML);
strValue = value;
element.attachEvent("onchange", fcnRestore);
}
else if(strTagname == "SELECT")
{
intSelectedIndex = selectedIndex;
element.attachEvent("onchange", fcnRestore);
}
}
function fcnRestore()
{
if(strTagname == "INPUT")
{
value = strValue ;
}
else if(strTagname == "TEXTAREA")
{
value = strValue;
}
else if(strTagname == "SELECT")
{
selectedIndex = intSelectedIndex;
}
}
</SCRIPT>
</PUBLIC:COMPONENT>