1,HTML页
头部注册:
<%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPrefix="UserControl" TagName="CeShiBu"%>
<%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/KaiFaBu.ascx" TagPrefix="UserControl" TagName="KaiFaBu"%>
控件摆放位置:
<div id="controlspan" runat=server>
</div>
2,C#后台
/// <summary>
/// 根据事项和分类决定显示用户控件中哪些要素
/// </summary>
/// <param name="strFL"></param>
/// <param name="strSX"></param>
private void setUserControlVisible(string strFL,string strSX)
{
if (ViewState["rzDeptName"].ToString() == "开发部")
{
//用户控件路径
UserControl KaiFaBuControl = (UserControl)Page.LoadControl("UserControl/KaiFaBu.ascx");
Type tc = KaiFaBuControl.GetType();
//用户控件中要调用的初始化方法
System.Reflection.MethodInfo m = tc.GetMethod("OnStart");
//调用初始化方法要传递的参数
object[] bjParas = new object[];
bjParas[] = strSX;
m.Invoke(KaiFaBuControl, bjParas); controlspan.Controls.Clear();
controlspan.Controls.Add(KaiFaBuControl);
}
if (ViewState["rzDeptName"].ToString() == "测试部")
{
UserControl KaiFaBuControl = (UserControl)Page.LoadControl("UserControl/CeShiBu.ascx");
Type tc = KaiFaBuControl.GetType();
System.Reflection.MethodInfo m = tc.GetMethod("OnStart");
object[] bjParas = new object[];
bjParas[] = strSX;
m.Invoke(KaiFaBuControl, bjParas); controlspan.Controls.Clear();
controlspan.Controls.Add(KaiFaBuControl);
}
}
在页面上动态改变strFL,strSX,可以动态加载不同的用户控件,或者加载用户控件中的不同元素。
关于页面上限制 数字小数点百分号意外的字符不能输入
<input type=text ID="labValue" class="labValue" runat="server" onkeyup="this.value=this.value.replace(/[^\d\.\%]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d\.\%]/g,'')" value='<%#Eval("Value")%>'></input>
如果在cs页面中获取html控件对象,并获取其值的方法:
string strValue = (gvInformationOperateGather.Rows[i].FindControl("labValue") as System.Web.UI.HtmlControls.HtmlInputControl).Value.Trim();
服务器控件:System.Web.UI.Controls.TextBox