【问题标题】:ASP.net access control in FormView ItemTemplateFormView ItemTemplate 中的 ASP.net 访问控制
【发布时间】:2011-05-05 15:19:50
【问题描述】:

我有一个带有项目模板的表单视图,里面有一个控件,是否可以访问该控件 OnDatabound,以便我可以将控件与数据绑定。我在这里以面板为例。

<cc1:LOEDFormView ID="FireFormView" runat="server" DataSourceID="DataSourceResults"     CssClass="EditForm" DataKeyNames="id" OnDatabound="FireFromView_Databound">
<ItemTemplate>

<asp:Panel ID ="pnl" runat="server"></asp:Panel>

</ItemTemplate>

</cc1:LOEDFormView>

【问题讨论】:

    标签: c# asp.net formview itemtemplate


    【解决方案1】:

    您还必须注意要查找的控件所在的项目模式。就像您在项目模板中的控件一样,它会像..

    if (FormView1.CurrentMode == FormViewMode.ReadOnly)
    {
    
      Panel pnl = (Panel)FormView1.FindControl("pnl");
    }
    

    【讨论】:

    • 确保 formview 具有行数据绑定,否则它将始终为空。检查您的选择语句和参数以及
    • 我正在尝试在 OnDataBound 事件方法中查找控件。它仍然是空的。当我从任何位置(page_load 等)手动调用 FormView.DataBind() 时,我通过调试发现它不为空。但是当 SqlDataSource 通过 SqlDataSourceID 属性自动绑定时,OnDataBound 中的 FormView 内不存在任何控件。荒谬的。即将废弃这整件事,只使用没有延迟的razor mvc。
    • 这是一个老问题,但对于更新的代码,请尝试寻找 FormView1.Row.FindControl("pnl")
    【解决方案2】:

    我在您的标记中没有看到标签,但看到了一个面板。所以要访问面板,

    试试

    Panel p = FireFormView.FindControl("pnl") as Panel;
    if(p != null)
    {
        ...
    }
    

    【讨论】:

      【解决方案3】:

      下面的这段代码解决了我的问题。尽管该示例访问一个标签,但它适用于大多数控件。您只需将DataBound 事件添加到您的FormView

      protected void FormView1_DataBound(object sender, EventArgs e)
      {
        //check the formview mode 
        if (FormView1.CurrentMode == FormViewMode.ReadOnly)
        {
          //Check the RowType to where the Control is placed
          if (FormView1.Row.RowType == DataControlRowType.DataRow)
          {
            Label label1 = (Label)UserProfileFormView.Row.Cells[0].FindControl("Label1");
            if (label1 != null)
            {
              label1.Text = "Your text";
            }
          }
        }
      }
      

      【讨论】:

        【解决方案4】:
            if (FireFormView.Row != null)
            {
                if (FireFormView.CurrentMode == FormViewMode.ReadOnly)
                {
                    Panel pnl = (Panel)FireFormView.FindControl("pnl");
                }
                else
                {
                    //FormView is not in readonly mode
                }
            }
            else
            {
                //formview not databound, check select statement and parameters.
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-10
          • 2014-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-08
          相关资源
          最近更新 更多