Friday, 27 September 2013

asp.net c# - Toggle display the child DataList

asp.net c# - Toggle display the child DataList

I am try to toogle the display subnet data(childList). This codes below
are working fine, but not able to toogle the display. When I click on the
(+) sign, the ChildList dataList expands and displays the list subset data
(Contact). However, I want to change the (+) to (-) when childList data
expanded. And when user clicks on (-), it will collapse back (the child
list disappear)
Any idea how to do this? an example code would be great since I am a
newbie of ASP.NET.
Thanks,
<table width="595px">
<asp:DataList BackColor="#ffffff" id="DataList1"
DataSourceID="dsCompanyList" runat="server" Width="100%"
DataKeyField="Company">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" Text="+"
CommandArgument='<%#Container.ItemIndex%>'
OnCommand="LinkButton1_Command"
></asp:LinkButton>
</td>
<td><%#Eval("Row")%></td>
<td><%#Eval("Company")%></td>
</tr>
<asp:Panel ID="pnlChildView" runat="server">
<asp:DataList ID="childList" runat="server" Width="100%">
<ItemTemplate>
<tr>
<td><%#Eval("FirstName")%></td>
<td><%#Eval("LastName")%></td>
</tr>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
</table>
Code Behind:
public partial class _CompanyList2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
//pass index of item in command argument
int itemIndex = Convert.ToInt32(e.CommandArgument);
//depending on your needs bind the details on demand
//or preload during ItemDataBound
Panel childViewPanel =
(Panel)DataList1.Items[itemIndex].FindControl("pnlChildView");
if (childViewPanel != null)
{
//toggle visibility of childViewPanel and bind child list if
panel is visible
DataList childList =
(DataList)childViewPanel.FindControl("childList");
if (childList != null)
{
//int keyValue = (int)DataList1.DataKeys[itemIndex];
string keyValue = (string)DataList1.DataKeys[itemIndex];
using (SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connApps"].ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT
FirstName, LastName FROM dbo.Import_CompanyContact
WHERE REPLACE(Company, '''', '') = '" + keyValue +
"'", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataReader dr = cmd.ExecuteReader())
{
childList.DataSource = dr;
childList.DataBind();
}
}
}
}
}
}
}

No comments:

Post a Comment