簡體   English   中英

FormView ItemTemplate中的ASP.net訪問控制

[英]ASP.net access control in FormView ItemTemplate

我有一個帶有控件的項目模板的表單視圖,是否可以訪問該控件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>

您必須注意項目模式以及您想要查找的控件。 就像你在物品模板中的控制一樣,那就像...

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

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

下面的代碼解決了我的問題。 雖然該示例訪問標簽,但它適用於大多數控件。 您只需要向FormView添加一個DataBound事件。

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";
      }
    }
  }
}

我沒有在您的標記中看到標簽,但看到了Panel。 所以要訪問面板,

嘗試

Panel p = FireFormView.FindControl("pnl") as Panel;
if(p != null)
{
    ...
}
    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.
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM