ASP.net access control in FormView ItemTemplate

泄露秘密 提交于 2019-12-22 09:17:22

问题


I have a form view with an item template with a control inside, is it possible to access that control OnDatabound so I can bind the control with data. I'm using a panel as an example here.

<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>

回答1:


You have to take care the item mode as well in which your control exist which you want to find. Like if your control in Item Template, then it would be like..

if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{

  Panel pnl = (Panel)FormView1.FindControl("pnl");
}



回答2:


I don't see a label in your markup but see a Panel. So to access the panel,

Try

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



回答3:


This code below solved my issue. Although the example accesses a label it applies to most controls. You simply have to add a DataBound event to your 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.
    }


来源:https://stackoverflow.com/questions/5900197/asp-net-access-control-in-formview-itemtemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!