DataList in DataList, how to do it???

M

Mirek Endys

I have DataList as part of DataList item. DataList in DataList. The parent
DataList working well including Edit command, that shows Edit template and
correctly bind the data into edit template (where is the child DataList)....

But in case I want to make Edit in this child DataList it is not working...
No edit template showed... :(

this is a code that i use for the child DataList... Edit command

// this is for child DataList...
protected void dlColumnMappings_EditCommand(object source,
DataListCommandEventArgs e)
{
DataList dlColumnMappings = (DataList)source;
dlColumnMappings.EditItemIndex = e.Item.ItemIndex;
dlColumnMappings.DataBind();
}


// this is for parent DataList.. it is working

protected void dlTransfers_EditCommand(object source,
DataListCommandEventArgs e)
{
dlTransfers.EditItemIndex = e.Item.ItemIndex;
dlTransfers.DataBind();
}
 
S

Steven Cheng[MSFT]

Hi Mirek,

Welcome to the MSDN newsgroup.

As for the DataList databinding problem you mentioned, are you using
ASP.NET 2.0 and bind those DataList contro with some DataSource controls?
For ASP.NET 1.1, we need to reattache the DataSource when we redo the
databinding on DataList. For ASP.NET 2.0, I've just performed some simple
tests on myside and the edit command can work correct for both the parent
and nested sub DataList, therefore I think there may exists something else
incorrect which cause the problem behavior. Have you checked the template
to see whether all the button(for edit ) is assigned the "CommandName" as
Edit. Here is a test page's aspx template and the related event handler in
code behind, you can have a look for reference if can help:

===========aspx============
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP
3 [ProductID], [CategoryID], [ProductName] FROM [Products]">
</asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server" Text='<%#
Eval("CategoryID") %>'></asp:Label><br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server" Text='<%#
Eval("CategoryName") %>'></asp:Label><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%#
Eval("Description") %>'></asp:Label><br />
<asp:Button ID="btnEdit" runat="server" CommandName="Edit"
Text="Edit" /><br />

</ItemTemplate>
<EditItemTemplate>
<asp:DataList ID="DataList2" runat="server"
DataSourceID="SqlDataSource2" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("ProductID") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("ProductName") %>'></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("CategoryID") %>'></asp:Label>
<asp:Button ID="btnEdit" runat="server"
CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("ProductID") %>'></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#
Bind("ProductName") %>'></asp:TextBox>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
<asp:TextBox ID="TextBox3" runat="server" Text='<%#
Bind("CategoryID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:DataList>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:DataList></div>

========code events=========

protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":

DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();

break;

case "Cancel":

DataList1.EditItemIndex = -1;
DataList1.DataBind();

break;
}
}
protected void DataList2_ItemCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = source as DataList;
switch (e.CommandName)
{
case "Edit":

dl.EditItemIndex = e.Item.ItemIndex;
dl.DataBind();

break;

case "Cancel":

dl.EditItemIndex = -1;
dl.DataBind();

break;
}
}


Hope these help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Mirek Endys

Hi Steven,

The Transfers is List<Transfer>... Transfer object contain
List<DataColumnMapping>

This bound data into my child DataList , but the Edit command shows empty
template...
DataSource='<%# DataBinder.Eval(Container.DataItem, "DataColumnsMapping")
%>'
here is my code:


<asp:DataList ID="dlTransfers" runat="server" DataSource=<%# Transfers %>
OnItemCommand="dlTransfers_ItemCommand"
OnEditCommand="dlTransfers_EditCommand">
<ItemTemplate>
<asp:LinkButton ID="lbCaption" runat="server" CommandName="Edit"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>' Text='<%#
DataBinder.Eval(Container.DataItem, "Name") %>'></asp:LinkButton></td>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="593px" Text='<%#
DataBinder.Eval(Container.DataItem, "Name") %>'></asp:TextBox>
<asp:DataList ID="dlColumnMappings" runat="server" DataSource='<%#
DataBinder.Eval(Container.DataItem, "DataColumnsMapping") %>'
OnEditCommand="dlColumnMappings_EditCommand">
<ItemTemplate>
<table>
<tr>
<td id="tdSourceColumnName" runat="server" align="left" nowrap="nowrap"
valign="top" style="height: 12px">
<%# DataBinder.Eval(Container.DataItem, "SourceColumnName") %>
</td>
<td id="tdDestinationColumnName" runat="server" align="left" nowrap="nowrap"
valign="top" style="height: 12px">
<%# DataBinder.Eval(Container.DataItem, "DestinationColumnName") %>
</td>
<td>
<asp:LinkButton ID="lbEdit" runat="server"
CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSourceColumnName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "SourceColumnName") %>'></asp:TextBox>
<asp:TextBox ID="txtDestinationClumnName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "DestinationColumnName")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:DataList>
</EditItemTemplate>
</asp:DataList>




Steven Cheng said:
Hi Mirek,

Welcome to the MSDN newsgroup.

As for the DataList databinding problem you mentioned, are you using
ASP.NET 2.0 and bind those DataList contro with some DataSource controls?
For ASP.NET 1.1, we need to reattache the DataSource when we redo the
databinding on DataList. For ASP.NET 2.0, I've just performed some simple
tests on myside and the edit command can work correct for both the parent
and nested sub DataList, therefore I think there may exists something else
incorrect which cause the problem behavior. Have you checked the template
to see whether all the button(for edit ) is assigned the "CommandName" as
Edit. Here is a test page's aspx template and the related event handler
in
code behind, you can have a look for reference if can help:

===========aspx============
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP
3 [ProductID], [CategoryID], [ProductName] FROM [Products]">
</asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server" Text='<%#
Eval("CategoryID") %>'></asp:Label><br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server" Text='<%#
Eval("CategoryName") %>'></asp:Label><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%#
Eval("Description") %>'></asp:Label><br />
<asp:Button ID="btnEdit" runat="server" CommandName="Edit"
Text="Edit" /><br />

</ItemTemplate>
<EditItemTemplate>
<asp:DataList ID="DataList2" runat="server"
DataSourceID="SqlDataSource2" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("ProductID") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("ProductName") %>'></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("CategoryID") %>'></asp:Label>
<asp:Button ID="btnEdit" runat="server"
CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("ProductID") %>'></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#
Bind("ProductName") %>'></asp:TextBox>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
<asp:TextBox ID="TextBox3" runat="server" Text='<%#
Bind("CategoryID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:DataList>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:DataList></div>

========code events=========

protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":

DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();

break;

case "Cancel":

DataList1.EditItemIndex = -1;
DataList1.DataBind();

break;
}
}
protected void DataList2_ItemCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = source as DataList;
switch (e.CommandName)
{
case "Edit":

dl.EditItemIndex = e.Item.ItemIndex;
dl.DataBind();

break;

case "Cancel":

dl.EditItemIndex = -1;
dl.DataBind();

break;
}
}


Hope these help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Mirek Endys

Oh.. the problem is another...

The Transfers is List<Transfer>... single Transfer object contain
object DataMappings that is List<DataColumnMapping>.

DataList dlTransfers contains items, that contain DataList dlColumnMappings

The problem is in this hiearchical Binding i think...

dlTransfers.DataSource = Transfers
dlColumnMappings.DataSource = DataBinder.Eval(Container.DataItem,
"ColumnMappings");


protected void dlTransfers_EditCommand(object source,
DataListCommandEventArgs e)
{
dlTransfers.EditItemIndex = e.Item.ItemIndex;
dlTransfers.DataBind();
}

protected void dlColumnMappings_EditCommand(object source,
DataListCommandEventArgs e)
{
DataList dlColumnMappings = (DataList)source as DataList;
dlColumnMappings.EditItemIndex = e.Item.ItemIndex;

// in case I use this it shows me empty dlColumnMappings, because of
the dlTransfer was not Binded
// dlColumnMappings.DataBind();

// in case I use this it shows me binded dlColumnMappings, but not in
EditTemplate
// dlTransfers.DataBind();

}


Dont you know, how to use this hierarchical binding???

Thanks, Mirek.

Mirek Endys said:
Hi Steven,

The Transfers is List<Transfer>... Transfer object contain
List<DataColumnMapping>

This bound data into my child DataList , but the Edit command shows empty
template...
DataSource='<%# DataBinder.Eval(Container.DataItem, "DataColumnsMapping")
%>'
here is my code:


<asp:DataList ID="dlTransfers" runat="server" DataSource=<%# Transfers %>
OnItemCommand="dlTransfers_ItemCommand"
OnEditCommand="dlTransfers_EditCommand">
<ItemTemplate>
<asp:LinkButton ID="lbCaption" runat="server" CommandName="Edit"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
Text='<%# DataBinder.Eval(Container.DataItem, "Name")
%>'></asp:LinkButton></td>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="593px" Text='<%#
DataBinder.Eval(Container.DataItem, "Name") %>'></asp:TextBox>
<asp:DataList ID="dlColumnMappings" runat="server" DataSource='<%#
DataBinder.Eval(Container.DataItem, "DataColumnsMapping") %>'
OnEditCommand="dlColumnMappings_EditCommand">
<ItemTemplate>
<table>
<tr>
<td id="tdSourceColumnName" runat="server" align="left" nowrap="nowrap"
valign="top" style="height: 12px">
<%# DataBinder.Eval(Container.DataItem, "SourceColumnName") %>
</td>
<td id="tdDestinationColumnName" runat="server" align="left"
nowrap="nowrap" valign="top" style="height: 12px">
<%# DataBinder.Eval(Container.DataItem, "DestinationColumnName") %>
</td>
<td>
<asp:LinkButton ID="lbEdit" runat="server"
CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSourceColumnName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "SourceColumnName") %>'></asp:TextBox>
<asp:TextBox ID="txtDestinationClumnName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "DestinationColumnName")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:DataList>
</EditItemTemplate>
</asp:DataList>




Steven Cheng said:
Hi Mirek,

Welcome to the MSDN newsgroup.

As for the DataList databinding problem you mentioned, are you using
ASP.NET 2.0 and bind those DataList contro with some DataSource controls?
For ASP.NET 1.1, we need to reattache the DataSource when we redo the
databinding on DataList. For ASP.NET 2.0, I've just performed some simple
tests on myside and the edit command can work correct for both the parent
and nested sub DataList, therefore I think there may exists something
else
incorrect which cause the problem behavior. Have you checked the template
to see whether all the button(for edit ) is assigned the "CommandName" as
Edit. Here is a test page's aspx template and the related event handler
in
code behind, you can have a look for reference if can help:

===========aspx============
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="Data Source=localhost;Initial
Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP
3 [ProductID], [CategoryID], [ProductName] FROM [Products]">
</asp:SqlDataSource>
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server" Text='<%#
Eval("CategoryID") %>'></asp:Label><br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server" Text='<%#
Eval("CategoryName") %>'></asp:Label><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%#
Eval("Description") %>'></asp:Label><br />
<asp:Button ID="btnEdit" runat="server" CommandName="Edit"
Text="Edit" /><br />

</ItemTemplate>
<EditItemTemplate>
<asp:DataList ID="DataList2" runat="server"
DataSourceID="SqlDataSource2" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("ProductID") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("ProductName") %>'></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("CategoryID") %>'></asp:Label>
<asp:Button ID="btnEdit" runat="server"
CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%#
Bind("ProductID") %>'></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%#
Bind("ProductName") %>'></asp:TextBox>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
<asp:TextBox ID="TextBox3" runat="server"
Text='<%#
Bind("CategoryID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:DataList>
<asp:Button ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:DataList></div>

========code events=========

protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":

DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();

break;

case "Cancel":

DataList1.EditItemIndex = -1;
DataList1.DataBind();

break;
}
}
protected void DataList2_ItemCommand(object source,
DataListCommandEventArgs e)
{
DataList dl = source as DataList;
switch (e.CommandName)
{
case "Edit":

dl.EditItemIndex = e.Item.ItemIndex;
dl.DataBind();

break;

case "Cancel":

dl.EditItemIndex = -1;
dl.DataBind();

break;
}
}


Hope these help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top