【问题标题】:Can't find control in edit mode in DataList在 DataList 的编辑模式下找不到控件
【发布时间】:2010-11-08 05:54:03
【问题描述】:
 private void BindDataList()
{
        int userId = Convert.ToInt32(ProfileInfo.GetUserID());
        DataList1.DataSource = CatalogAccess.GetAddressByUserID(userId);
        DataList1.DataBind();
        foreach (DataListItem item in DataList1.Items)
        {
            Label lbl = (Label)item.FindControl("lbl");
            lbl.Text = "myLabel";
        }
    }

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        int userId = Convert.ToInt32(ProfileInfo.GetUserID());        
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataSource = CatalogAccess.GetAddressByUserID(userId);
        DataList1.DataBind();
        Label lbl = (Label)e.Item.FindControl("lbl") as Label;
        lbl.Text = "edit mode";
    }

<asp:DataList ID="DataList1" runat="server" oneditcommand="DataList1_EditCommand" >
        <ItemTemplate>
                    <asp:Label ID="lblAddressID" runat="server" Text='<%# Bind("addressID") %>'/>
                    <asp:Label ID="lbl" runat="server" />
                    <asp:Button runat="Server" ID="cmdEdit" CommandName="Edit" Text="Edit"/> 
            </ItemTemplate>            
            <EditItemTemplate>  
                    <asp:TextBox ID="txtAddressID" runat="server" Text='<%# Bind("addressID") %>' BackColor="#FFFF66" />        
                    <asp:Label ID="lbl" runat="server"/>
                    <asp:Button runat="Server" ID="cmdUpdate" CommandName="Update" Text="Update" />
                    <asp:Button runat="Server" ID="cmdCancel" CommandName="Cancel" Text="Cancel"/> 
            </EditItemTemplate>
       </asp:DataList>

【问题讨论】:

    标签: c# data-binding listview findcontrol


    【解决方案1】:

    第 1 步:在某处绑定数据

    第二步:处理 OnItemDataBound 事件,在这里找到你的控件,类似下面...

      protected void DataList1__ItemDataBound(Object sender, DataListItemEventArgs e)
      {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            Label lbl = (Label)e.Item.FindControl("lbl");
            lbl.Text = "edit mode";
        }
      }
    

    有关此活动的更多信息,请查看MSDN example。您必须检查 ItemType。在这种情况下,它处于编辑模式,否则您检查列表项或替代项等。

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多