I want to add a value to a row in a datalist. I have added a textbox and a LinkButton that shows up when the DataList is in edit mode.
The LinkButton has CommandName="Add" and CommandArgument with the id of the row as a value.
I have added a OnItemCommand to the DataList and in the code behind.
But when try to retrieve the text from the TextBox in codebehind it is empty.
Does any one have a clue to what might be wrong?
<asp:DataList ID="dl_courses" runat="server" DataKeyField="CourseId" OnItemCommand="dl_courses_OnItemCommand" OnItemDataBound="dl_courses_ItemDataBound" OnEditCommand="dl_courses_OnEditCommand" OnCancelCommand="dl_courses_OnCancelCommand" OnDeleteCommand="dl_courses_OnDeleteCommand" OnUpdateCommand="dl_courses_OnUpdateCommand">
<ItemTemplate>
<asp:LinkButton ID="lb_editItem" Text="Editera kursnamnet" CommandName="Edit" runat="server" /> <br /><asp:Label ID="lbl_courseId" Text='<%# DataBinder.Eval(Container.DataItem, "CourseId")%>' runat="server" /><br /><asp:Label ID="lbl_coursename" Text='<%# DataBinder.Eval(Container.DataItem, "CourseName")%>' runat="server" /><br />
<asp:DataList ID="DateList" runat="server">
<ItemTemplate>
<%# Container.DataItem%>
</ItemTemplate>
</asp:DataList>
<asp:TextBox ID="tb_addDate" Visible="false" runat="server" />
</ItemTemplate>
<ItemStyle BackColor="#BCBCBC" />
<AlternatingItemStyle BackColor="#FFFFFF" />
<EditItemTemplate>
<asp:TextBox ID="tb_coursename" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CourseName")%>' /> <asp:LinkButton ID="lb_cancel" Text="Avbryt" CommandName="Cancel" runat="server" /> <asp:LinkButton ID="lb_deleteCourse" Text="Radera" CommandName="Delete" runat="server" /> <asp:LinkButton ID="lb_updateCourse" Text="Uppdatera" CommandName="Update" runat="server" />
<br />
<asp:TextBox ID="tb_addDate" runat="server" /> <asp:LinkButton ID="lb_addNewDate" Text="Lägg till nytt datum" CommandName="Add" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "CourseId")%>' />
<br />
<asp:DataList ID="DateList" runat="server" OnEditCommand="DateList_OnEditCommand" OnUpdateCommand="DateList_OnUpdateCommand" OnCancelCommand="DateList_OnCancelCommand">
<ItemTemplate>
<asp:LinkButton ID="lb_editCourseItem" Text="Editera datum" CommandName="Edit" runat="server" /> <%# Container.DataItem%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb_courseDate" Text='<%# Container.DataItem%>' runat="server" /><asp:LinkButton ID="lb_cancel" Text="Avbryt" CommandName="Cancel" runat="server" /> <asp:LinkButton ID="lb_updateDate" Text="Uppdatera" CommandName="Update" runat="server" /> <%--CommandArgument='<%# DataBinder.Eval(Container.DataItem, "CourseId")%>'--%>
</EditItemTemplate>
</asp:DataList>
<br />
</EditItemTemplate>
</asp:DataList>
From CodeBehind
protected void dl_courses_OnItemCommand(Object sender, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Add") && e.Item.ItemType == ListItemType.EditItem)
{
TextBox tb = (TextBox)e.Item.FindControl("tb_coursename");
string courseDate = ((TextBox)e.Item.FindControl("tb_addDate")).Text;
Guid courseId = new Guid(e.CommandArgument.ToString());
Course course = RepositoryFactory.CreateCourseRepository().FetchSingle(new Course(null, null, courseId));
if (IsDate(courseDate))
{
//course.CourseDate.Add(Convert.ToDateTime(tb_date.Text));
course.CourseDate.Add(DateTime.Parse(courseDate));
updateCourse(course);
DataList innerList = (DataList)e.Item.FindControl("DateList");
innerList.EditItemIndex = e.Item.ItemIndex;
innerList.DataBind();
//tb_date.Text = "";
lbl_message.Text = "";
}
else
lbl_message.Text = "Felaktigt datum. Formatet måste vara åååå-mm-dd och datumet måste vara efter dagens datum.<br><br>";
}
}