2009-07-05 3 views
1
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> 

答えて

1

ステップ1 DataListコントロールで編集モードでコントロールを見つけることができません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を確認する必要があります。この場合、それは編集モードです。そうでなければ、リストアイテムまたは代替アイテムなどを確認します。

+0

Works、ありがとう –