FormView ItemTemplate 中的 ASP.net 访问控制

发布于 2024-11-05 10:21:41 字数 410 浏览 7 评论 0原文

我有一个表单视图,其中包含一个带有控件的项目模板,是否可以访问该控件 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阿楠 2024-11-12 10:21:42

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

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

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

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");
}
无人问我粥可暖 2024-11-12 10:21:42

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

请尝试

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

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)
{
    ...
}
狼性发作 2024-11-12 10:21:42
    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.
    }
    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.
    }
我还不会笑 2024-11-12 10:21:42

下面的代码解决了我的问题。尽管该示例访问标签,但它适用于大多数控件。您只需将 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";
      }
    }
  }
}

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";
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文