如何根据某些条件在formview中隐藏和显示项目模板?

时间:2013-11-18 12:16:12

标签: asp.net session formview

这里我从前一页使用session获取一些值来检查条件:

  string PTN = Session["PrimaryTool"].ToString();
  string PTE = Session["PrimaryToolExp"].ToString();
  if (PTN != null && PTE != null)
  {
     if (fvJobApplying.CurrentMode == FormViewMode.ReadOnly)
     {
         Label PT = (Label)fvJobApplying.FindControl("lblPrimaryTool");
         PT.Visible = true;

         Label PTExperience = (Label)fvJobApplying.FindControl("lblPrimaryToolExp");
         PTExperience.Visible = true;
         Label Experience = (Label)fvJobApplying.FindControl("lblExperience");
         Experience.Visible = false;
     }
}

我的会话值和我的条件正常...这里取决于条件如何在asp.net中的formview中显示和隐藏列或项模板

1 个答案:

答案 0 :(得分:0)

目前还不清楚你实际上有什么问题/错误......

请勿使用Page_Load绑定或访问您的FormView,而是使用FormView的{​​{1}}事件和CurrentMode property

DataBound

所以在那里找到你的标签并设置所需的protected void fvJobApplying_DataBound(object sender, System.EventArgs e) { if(fvJobApplying.CurrentMode == FormViewMode.ReadOnly) { // here you can safely access the FormView's ItemTemplate and it's controls via FindControl Label PT = (Label)fvJobApplying.FindControl("lblPrimaryTool"); PT.Visible = true; } else if(fvJobApplying.CurrentMode == FormViewMode.Edit) { // here you can safely access the FormView's EditItemTemplate and it's controls via FindControl } else if(fvJobApplying.CurrentMode == FormViewMode.Insert) { // here you can safely access the FormView's InsertItemTemplate and it's controls via FindControl } } 。当您Visibility DataBind

时会触发此事件