在 DataList 中找不到编辑模式下的控件

发布于 2024-07-26 15:51:28 字数 1772 浏览 4 评论 0 原文

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

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

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

发布评论

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

评论(1

梨涡 2024-08-02 15:51:28

第 1 步:在某处绑定数据

第 2 步:处理 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 示例。 您必须检查 ItemType。 在这种情况下,它处于编辑模式,否则您检查列表项或备用项等。

Step 1: bind data somewhere

Step 2: handle the OnItemDataBound event and find your control here, similar to the following...

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

For more info on this event take a look at the MSDN example. You have to check for the ItemType. In this case it is in edit mode, otherwise you check for a listitem or alternate item etc.

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