我有一个项目列表,我想通过Page_Load()上的cookie设置。这是一个非常长的Web表单在SharePoint之上。我不想为每个控件的事件调用每个方法,但这正是我现在正在做的事情。换句话说,我不需要知道为页面中的每个控件调用什么方法,我只需要调用它。
*编辑 - 对于每个控件,autopostback也设置为true,我使用Page.IsPostback处理页面加载中的事件。我在某处读到了selectindexchanged事件不会从pageload中触发,所以这就是为什么我一直在调查丑陋的解决方法。
当前代码:
enter code here
foreach (Control C in GPC.GetAllControls(this.Master.FindControl("PlaceHolderMain").Controls))
{
if (C == null) continue;
if (C is TextBox)
{
TextBox T = (TextBox)C;
if (cookie.Values[C.ID] != null)
{
T.Text = cookie.Values[C.ID];
}
}
else if (C is DropDownList)
{
DropDownList D = (DropDownList)C;
if (cookie.Values[C.ID] != null)
{
D.SelectedIndex = int.Parse(cookie.Values[C.ID]);
switch (C.ID)
{
case "DropDownListCategory":
onChangeCategory(sender, e);
break;
case "DropDownListContractType":
DropDownListContractType_SelectedIndexChanged(sender, e);
break;
case "DropDownListITPurchase":
onChangeITPurchase(sender, e);
break;
case "DropDownListServiceContract":
onChangeServiceContract(sender, e);
break;
case "DropDownListPOM":
onChangePOMChoice(sender, e);
break;
//ETC.
}
}
}
今天早上放弃了一些过去的尝试,每个尝试都在一些评论分隔符中:
/*
D.AutoPostBack = true;
D.SelectedIndex = 0;
System.Threading.Thread T = new System.Threading.Thread(delegate() {
System.Threading.Thread.Sleep(10);
D.SelectedIndex = int.Parse(cookie.Values[C.ID]);
});
T.Start();
*/
/* this.ClientScript.RegisterStartupScript(
this.GetType(),
"ValidateAndStore",
"<script language='javascript' type='text/javascript'>" +
"$('[id$=" + D.ID + "]').prop('selectedIndex',"+int.Parse(cookie.Values[C.ID])+");" +
"</script>"); */
/* this.ClientScript.RegisterStartupScript(
this.GetType(),
"ValidateAndStore",
"<script language='javascript' type='text/javascript'>" +
"$('[id$=" + D.ID + "]').selectedIndex=" + int.Parse(cookie.Values[C.ID]) + ";" +
"__doPostBack(ctl00_PlaceHolderMain_" + D.ID + ",'onchange');" +
"</script>");*/