D
Dave
I'm relatively new to ASP.NET and I'm having a problem editing data in
a DataList. I have a DataGrid that lists fieldand I have a DataList
that displays detailed information about each item. I want to use the
DataList to edit the fields.
When I click on the EditCommandColumn button on the first record, the
DataList displays the EditItemTemplates (as it should). However, when
I try to edit any other item in the DataGrid, the corresponding
DataList only displays in the regular ItemTemplate and is uneditable.
Any help would be greatly apprectiated. Below is a copy of the aspx
and the code behind.
Thanks in advance,
Dave
<!-- Item information -->
<asp:datalist id="dlLPDetail" EnableViewState="True"
OnItemCommand="DoDetItem" OnCancelCommand="DoItemCancel"
OnUpdateCommand="DoItemUpdate" RepeatLayout="Table" Runat="server"
HorizontalAlign="Center"
Visible="True">
<ItemTemplate>
<asp:Label Runat=server Visible=True Enabled=True>Regular ID:
</asp:Label>
<asp:TextBox ID="Textbox1" visible="true" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "ID_LP") %>' ReadOnly="True"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label Runat=server Visible=True>Edit ID: </asp:Label>
<asp:TextBox ID="tbID" visible="true" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "ID_LP") %>' ReadOnly="True"/>
<asp:Table ID="tblLDDetail" width="729" Runat="server"
EnableViewState="True" HorizontalAlign="Left" Visible="True">
<asp:TableRow>
<asp:TableCell>Title:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbTitle" runat="server" Width="400" Text='<%#
DataBinder.Eval(Container.DataItem, "Title") %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Body:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbBody" runat="server" Width="400" Height="150"
TextMode="MultiLine" Wrap="True" Columns="2" Text='<%#
DataBinder.Eval(Container.DataItem, "body") %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Status:</asp:TableCell>
<asp:TableCell>
<aspropDownList Runat="server" ID="lstStatus"
DataValueField="ID_Status" DataTextField="Status" DataSource="<%#
GetStatuses() %>" SelectedIndex='<%#
GetSelIndex((string)DataBinder.Eval(Container.DataItem, "Status")) %>'
/>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Post Date:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbPostDate" runat="server" Text='<%#
String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"PostDate")) %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Expiration Date: </asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbDueDate" runat="server" Text='<%#
String.Format("{0:d}", DataBinder.Eval(Container.DataItem, "DueDate"))
%>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="btnLPDetUpdt" Text="Update" CommandName="Update"
Runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnLPDetCan" Text="Cancel" CommandName="Cancel"
Runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:ImageButton ID="imgCalPD" visible="False" Runat="server"
ImageUrl="images/calendar.gif" OnClick="calPostDate_click" />
<asp:Calendar ID="calPostDate" visible="False" Runat="server"
ShowNextPrevMonth="True" SelectedDate='<%#
DataBinder.Eval(Container.DataItem, "PostDate") %>' />
</EditItemTemplate>
</asp:datalist>
<P></P>
<P>
<asp:Label ID="lblEmptyDG" Runat="server"><br>There are no records to
display. Please click the Add button to add a Legal
Posting.<br><br></asp:Label>
<!-- List Data Grid -->
<asp:datagrid runat="server" id="LPDataGrid"
AutoGenerateColumns="False"
OnEditCommand="DoItemEdit" OnDeleteCommand="DoItemDelete"
OnSortCommand="DoItemSort"
OnItemCommand="DoItemDetails" OnItemDataBound="BindDataItem"
GridLines="Both" CellPadding="5" BackColor="White" BorderWidth="1px"
BorderStyle="None" AllowPaging="true" AllowCustomPaging="True"
ShowHeader="True" ShowFooter="True"
AllowSorting="True" DataMember="Legal_Post" DataKeyField="ID_LP"
DataSource="<%# dsLP %>"
EnableViewState="True" HorizontalAlign="Center" BorderColor="White">
<SelectedItemStyle Font-Bold="True"></SelectedItemStyle>
<EditItemStyle BorderStyle="Outset"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle BackColor="DarkGray"></HeaderStyle>
<FooterStyle BackColor="DarkGray"></FooterStyle>
<Columns>
<asp:ButtonColumn Visible="false" CommandName="DoItemDetails"
Text="Details" ButtonType="PushButton"/>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit Info"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="ID_LP" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:TemplateColumn SortExpression="Title ASC" HeaderText="Title">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="PostDate" HeaderText="Post
Date">
<ItemTemplate>
<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"PostDate")) %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="DueDate" HeaderText="Expiration
Date">
<ItemTemplate>
<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"DueDate")) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" Position="TopAndBottom"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>
************************************************************************
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack != true)
{
dlLPDetail.Visible = false;
BindData(strSort);
}
}
private void BindData(string s)
{
//Binds data to the DataGrid successfully.
}
private void BindDetailData(int idLP)
{
//Binds data to the DataList (record details).
SqlCommand sqlComm = new SqlCommand(strSqlComm, sqlConn);
DataTable dt = new DataTable("LP_Detail");
SqlDataAdapter sqlLPDetAdapter = new SqlDataAdapter(sqlComm);
sqlConn.ConnectionString = strSqlConn;
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@idLP", SqlDbType.Int).Value = idLP;
sqlConn.Open();
sqlLPDetAdapter.Fill(dsLP, "LP_Detail");
this.dlLPDetail.DataSource = dsLP.Tables["LP_Detail"];
this.dlLPDetail.DataMember = "LP_Detail";
this.dlLPDetail.DataBind();
sqlConn.Close();
}
public void DoItemEdit(Object objSource, DataGridCommandEventArgs e)
{
dsplyDataListItem();
string id = LPDataGrid.DataKeys[e.Item.ItemIndex].ToString();
int idLP = Int32.Parse(id);
this.dlLPDetail.EditItemIndex = e.Item.ItemIndex;
BindDetailData(idLP);
Response.Write((string)dlLPDetail.EditItemIndex.ToString());
ViewState["Update"] = "Edit";
}
private void dsplyDataListItem()
{
this.LPDataGrid.Visible = false;
this.dlLPDetail.Visible = true;
}
a DataList. I have a DataGrid that lists fieldand I have a DataList
that displays detailed information about each item. I want to use the
DataList to edit the fields.
When I click on the EditCommandColumn button on the first record, the
DataList displays the EditItemTemplates (as it should). However, when
I try to edit any other item in the DataGrid, the corresponding
DataList only displays in the regular ItemTemplate and is uneditable.
Any help would be greatly apprectiated. Below is a copy of the aspx
and the code behind.
Thanks in advance,
Dave
<!-- Item information -->
<asp:datalist id="dlLPDetail" EnableViewState="True"
OnItemCommand="DoDetItem" OnCancelCommand="DoItemCancel"
OnUpdateCommand="DoItemUpdate" RepeatLayout="Table" Runat="server"
HorizontalAlign="Center"
Visible="True">
<ItemTemplate>
<asp:Label Runat=server Visible=True Enabled=True>Regular ID:
</asp:Label>
<asp:TextBox ID="Textbox1" visible="true" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "ID_LP") %>' ReadOnly="True"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label Runat=server Visible=True>Edit ID: </asp:Label>
<asp:TextBox ID="tbID" visible="true" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "ID_LP") %>' ReadOnly="True"/>
<asp:Table ID="tblLDDetail" width="729" Runat="server"
EnableViewState="True" HorizontalAlign="Left" Visible="True">
<asp:TableRow>
<asp:TableCell>Title:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbTitle" runat="server" Width="400" Text='<%#
DataBinder.Eval(Container.DataItem, "Title") %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Body:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbBody" runat="server" Width="400" Height="150"
TextMode="MultiLine" Wrap="True" Columns="2" Text='<%#
DataBinder.Eval(Container.DataItem, "body") %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Status:</asp:TableCell>
<asp:TableCell>
<aspropDownList Runat="server" ID="lstStatus"
DataValueField="ID_Status" DataTextField="Status" DataSource="<%#
GetStatuses() %>" SelectedIndex='<%#
GetSelIndex((string)DataBinder.Eval(Container.DataItem, "Status")) %>'
/>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Post Date:</asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbPostDate" runat="server" Text='<%#
String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"PostDate")) %>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Expiration Date: </asp:TableCell>
<asp:TableCell>
<asp:TextBox id="tbDueDate" runat="server" Text='<%#
String.Format("{0:d}", DataBinder.Eval(Container.DataItem, "DueDate"))
%>' />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="btnLPDetUpdt" Text="Update" CommandName="Update"
Runat="server" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="btnLPDetCan" Text="Cancel" CommandName="Cancel"
Runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:ImageButton ID="imgCalPD" visible="False" Runat="server"
ImageUrl="images/calendar.gif" OnClick="calPostDate_click" />
<asp:Calendar ID="calPostDate" visible="False" Runat="server"
ShowNextPrevMonth="True" SelectedDate='<%#
DataBinder.Eval(Container.DataItem, "PostDate") %>' />
</EditItemTemplate>
</asp:datalist>
<P></P>
<P>
<asp:Label ID="lblEmptyDG" Runat="server"><br>There are no records to
display. Please click the Add button to add a Legal
Posting.<br><br></asp:Label>
<!-- List Data Grid -->
<asp:datagrid runat="server" id="LPDataGrid"
AutoGenerateColumns="False"
OnEditCommand="DoItemEdit" OnDeleteCommand="DoItemDelete"
OnSortCommand="DoItemSort"
OnItemCommand="DoItemDetails" OnItemDataBound="BindDataItem"
GridLines="Both" CellPadding="5" BackColor="White" BorderWidth="1px"
BorderStyle="None" AllowPaging="true" AllowCustomPaging="True"
ShowHeader="True" ShowFooter="True"
AllowSorting="True" DataMember="Legal_Post" DataKeyField="ID_LP"
DataSource="<%# dsLP %>"
EnableViewState="True" HorizontalAlign="Center" BorderColor="White">
<SelectedItemStyle Font-Bold="True"></SelectedItemStyle>
<EditItemStyle BorderStyle="Outset"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle BackColor="DarkGray"></HeaderStyle>
<FooterStyle BackColor="DarkGray"></FooterStyle>
<Columns>
<asp:ButtonColumn Visible="false" CommandName="DoItemDetails"
Text="Details" ButtonType="PushButton"/>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit Info"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="ID_LP" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:TemplateColumn SortExpression="Title ASC" HeaderText="Title">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="PostDate" HeaderText="Post
Date">
<ItemTemplate>
<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"PostDate")) %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="DueDate" HeaderText="Expiration
Date">
<ItemTemplate>
<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem,
"DueDate")) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" Position="TopAndBottom"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>
************************************************************************
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack != true)
{
dlLPDetail.Visible = false;
BindData(strSort);
}
}
private void BindData(string s)
{
//Binds data to the DataGrid successfully.
}
private void BindDetailData(int idLP)
{
//Binds data to the DataList (record details).
SqlCommand sqlComm = new SqlCommand(strSqlComm, sqlConn);
DataTable dt = new DataTable("LP_Detail");
SqlDataAdapter sqlLPDetAdapter = new SqlDataAdapter(sqlComm);
sqlConn.ConnectionString = strSqlConn;
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@idLP", SqlDbType.Int).Value = idLP;
sqlConn.Open();
sqlLPDetAdapter.Fill(dsLP, "LP_Detail");
this.dlLPDetail.DataSource = dsLP.Tables["LP_Detail"];
this.dlLPDetail.DataMember = "LP_Detail";
this.dlLPDetail.DataBind();
sqlConn.Close();
}
public void DoItemEdit(Object objSource, DataGridCommandEventArgs e)
{
dsplyDataListItem();
string id = LPDataGrid.DataKeys[e.Item.ItemIndex].ToString();
int idLP = Int32.Parse(id);
this.dlLPDetail.EditItemIndex = e.Item.ItemIndex;
BindDetailData(idLP);
Response.Write((string)dlLPDetail.EditItemIndex.ToString());
ViewState["Update"] = "Edit";
}
private void dsplyDataListItem()
{
this.LPDataGrid.Visible = false;
this.dlLPDetail.Visible = true;
}