Edit, Update, Delete on Nested DataList in ASP.NET

The sample code, consiting of .aspx page and code-behind c# file, explains how various operations (edit, update, delete) can be performed on a nested datalist control in ASP.NET

The aspx page example follows:


<asp:DataList ID="DList1" runat="server" DataKeyField="country_id" OnItemCommand="DataList1_ItemCommand" Height="100%" Width="100%" >
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="showstates" Text='<%# Eval("country_name") % >
</asp:LinkButton >
<asp:DataList ID="DList2" runat="server" DataKeyField="state_id" OnDeleteCommand="DList2_DeleteCommand" Visible="False" Width="220px" OnEditCommand="DList2_EditCommand" OnUpdateCommand="DList2_UpdateCommand" OnCancelCommand="DList2_CancelCommand" >
<ItemTemplate >
<table>
<tr>
<td style="width: 218px >
%LT; asp:Label ID="Label3" runat="server" Text='<%# Eval("state_name") %>' Width="133px">
</asp:Label>
</td%RT;
<td style="width: 78px">
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit">Edit
</asp:LinkButton>
</td>
<td style="width: 82px">
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="delete">Delete
</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderTemplate>
State
</HeaderTemplate
<EditItemTemplate
<table>
<tr>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("state_name")%>' >
</asp:TextBox>
/td>
<td>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="update">Update</asp:LinkButton>
LT;/td>
<td>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="cancel">Cancel</asp:LinkButton</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
<asp:Label ID="Label4" runat="server"></asp:Label>
</ItemTemplate>
<HeaderTemplate>
Country
</HeaderTemplate>
</asp:DataList>


The following code sample is the .cs c# code


protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "showstates")
{
bindcountry();
for (int i = 0; i < DList1.Items.Count; i++)
{
DataList dl1 = (DataList)DList1.Items[i].FindControl("DList2");
dl1.Visible = false;
Label lbl = (Label)DList1.Items[i].FindControl("Label4");
lbl.Visible = false;
}

DataList dl = (DataList)DList1.Items[e.Item.ItemIndex].FindControl("DList2");
loc.get_countryId = Int32.Parse(DList1.DataKeys[e.Item.ItemIndex].ToString());
DataSet ds = loc.selectAllStates();
if (ds.Tables[0].Rows.Count == 0)
{
Label lbl = (Label)DList1.Items[e.Item.ItemIndex].FindControl("Label4");
lbl.Text = "No Entry Found";
lbl.Visible = true;
}
else
{
dl.DataSource = ds;
dl.DataBind();
dl.Visible = true;
}
}
}
protected void DList2_DeleteCommand(object source, DataListCommandEventArgs e)
{
int i;
for (i = 0; i < DList1.Items.Count; i++)
{
DataList dl1 = (DataList)DList1.Items[i].FindControl("DList2");
if(Int32.Parse(dl1.Items.Count.ToString())>0)
break;
}
DataList dl = (DataList)DList1.Items[i].FindControl("DList2");
loc.get_stateId =Int32.Parse(dl.DataKeys[e.Item.ItemIndex].ToString());
loc.deleteState();
bindcountry();
}

protected void DList2_EditCommand(object source, DataListCommandEventArgs e)
{
int i;
for (i = 0; i < DList1.Items.Count; i++)
{
DataList dl1 = (DataList)DList1.Items[i].FindControl("DList2");
if (Int32.Parse(dl1.Items.Count.ToString()) > 0)
break;
}
DataList dl = (DataList)DList1.Items[i].FindControl("DList2");
dl.EditItemIndex = e.Item.ItemIndex;
loc.get_countryId = Int32.Parse(DList1.DataKeys[i].ToString());
DataSet ds = loc.selectAllStates();
dl.DataSource = ds;
dl.DataBind();
}
protected void DList2_UpdateCommand(object source, DataListCommandEventArgs e)
{
int i;
for (i = 0; i < DList1.Items.Count; i++)
{
DataList dl1 = (DataList)DList1.Items[i].FindControl("DList2");
if (Int32.Parse(dl1.Items.Count.ToString()) > 0)
break;
}
DataList dl = (DataList)DList1.Items[i].FindControl("DList2");
loc.get_stateId = Int32.Parse(dl.DataKeys[e.Item.ItemIndex].ToString());
TextBox txt = (TextBox)dl.Items[e.Item.ItemIndex].FindControl("TextBox2");
if (txt.Text == "")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "hh", "<html><script>alert('State Name Cannot be empty')</script></html>");
}
else
{
loc.get_stateName = txt.Text;
loc.updateState();
dl.EditItemIndex = -1;
}

loc.get_countryId = Int32.Parse(DList1.DataKeys[i].ToString());
DataSet ds = loc.selectAllStates();
dl.DataSource = ds;
dl.DataBind();
}
protected void DList2_CancelCommand(object source, DataListCommandEventArgs e)
{
int i;
for (i = 0; i < DList1.Items.Count; i++)
{
DataList dl1 = (DataList)DList1.Items[i].FindControl("DList2");
if (Int32.Parse(dl1.Items.Count.ToString()) > 0)
break;
}
DataList dl = (DataList)DList1.Items[i].FindControl("DList2");
loc.get_stateId = Int32.Parse(dl.DataKeys[e.Item.ItemIndex].ToString());
dl.EditItemIndex = -1;

loc.get_countryId = Int32.Parse(DList1.DataKeys[i].ToString());
DataSet ds = loc.selectAllStates();
dl.DataSource = ds;
dl.DataBind();
}


Comments

Guest Author: nicola15 Oct 2012

i have error on loc. ??

Author: Nicola15 Oct 2012 Member Level: Bronze   Points : 2

hi there! im on your example... but DList2_EditCommand dosent work.
My example is a bit different. I have grdiview and inside nested a DataList.. delete, insert in datalist (nested inside gridview) works well... but edit command dosent work.. i cant get datalist control that i want to edit.. i click on the second row.. edit the fist item. can u help me about on my OnEditCommand (OnEditCommand="GV_Crews_Edit") thank you very much.
Nicola



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: