DataList ItemTemplate and EditItemTemplate

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>
<asp:DropDownList 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;
}
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for posting in this group.
Based on my understanding, your application works well when you want to
edit the first datagrid row. After you finished first row editing and
return to the datagrid(through click update button or cancel button), you
want to edit the other rows, but at this time datalist edit failed.
After reviewing your sample code, I think you did not paste out all the
related code. You did not paste out the code of datalist's
OnItemCommand="DoDetItem" OnCancelCommand="DoItemCancel"
OnUpdateCommand="DoItemUpdate".
I have modified your sample code, and add the cancel and upate event
handler for your datalist:

protected void DoItemCancel(object sender, DataListCommandEventArgs e)
{
dlLPDetail.EditItemIndex=-1;
dsplyDataGrid();
}

protected void DoItemUpdate(object sender, DataListCommandEventArgs e)
{
dlLPDetail.EditItemIndex=-1;
dsplyDataGrid();
}

private void dsplyDataGrid()
{
this.LPDataGrid.Visible = true;
this.dlLPDetail.Visible = false;
}

private void dsplyDataListItem()
{
this.LPDataGrid.Visible = false;
this.dlLPDetail.Visible = true;
}

It works well through these 2 event handlers. But for your appliation, I
think you should add more logic into the update event handler(update the
input data into the database).

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Dave Jansen

Jeffrey,

Thanks for the quick response. I did leave off a bit of code on my
original post. I've included all of it here.

The problem I'm still having is that the only row in my datagrid I can
display in edit mode in the datalist is the first. The order I try to
edit them makes no difference. If I click on the 4th row first, it
displays the ItemTemplate. Only when I click on the 1st row of the
datagrid will it display in the EditItemTemplate.

I stepped through the code and the exact same code appears to be
executing both times, but clearly there is something I'm missing.

Thanks again,

Dave

<form id="Form1" method="post" runat="server">
<!-- 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>
<asp:DropDownList 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>
</EditItemTemplate>
</asp:datalist>
<P></P>
<P>
<table align="center" width="729">
<tr>
<td align="left">
<asp:Button ID="btnAddTop" Text=" Add " Runat="server"
OnClick="addLP" /></td>
</tr>
<tr>
<td>
<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>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddBot" Text=" Add " Runat="server"
OnClick="addLP" />
</td>
</tr>
</table>
</P>
<P>&nbsp;</P>
</form>

**********************************************************

private void Page_Load(object sender, System.EventArgs e)
{
try
{
if (Page.IsPostBack != true)
{
dlLPDetail.Visible = false;
BindData(strSort);
}
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>PageLoad");
}
}

private void BindData(string s)
{
try
{
sqlConn.ConnectionString = strSqlConn;
sqlConn.Open();
DataTable dt = GetLPs(s);
LPDataGrid.DataSource = dsLP;
LPDataGrid.DataMember = "Legal_Post";
LPDataGrid.DataBind();
sqlConn.Close();

if(dt.Rows.Count == 0)
{
this.LPDataGrid.Visible = false;
this.lblEmptyDG.Visible = true;
}
else
{
this.lblEmptyDG.Visible = false;
}

}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>BindData");
}
}

private DataTable GetLPs(string s)
{
try
{
string strSqlComm = "sp_LP_Select";
if(sqlConn.State == ConnectionState.Closed)
{
sqlConn.ConnectionString = strSqlConn;
}
SqlCommand sqlComm = new SqlCommand(strSqlComm, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add("@strSort", SqlDbType.NVarChar, 100).Value = s;
SqlDataAdapter sqlLPAdapter = new SqlDataAdapter(sqlComm);
sqlLPAdapter.Fill(dsLP, "Legal_Post");
DataTable dt = dsLP.Tables["Legal_Post"];
return dt;
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>GetLP");
DataTable dt = new DataTable();
return dt;
}
}

private void BindDetailData(int idLP)
{
try
{
strSqlComm = "sp_LP_Select_Det";
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();
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>BindDetailData");
}
}


public void BindDataItem(object s, DataGridItemEventArgs e)
{
try
{
if ((e.Item.ItemType != ListItemType.Header) & (e.Item.ItemType !=
ListItemType.Footer))
{
string confirm = "javascript:return confirm('Are you sure you want to
delete this record?')";
Button deleteB = (Button)(e.Item.Cells[2].Controls[0]);
deleteB.Attributes.Add("onclick", confirm);
}
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>BindDataItem");
}
}


public void addLP(object o, EventArgs e)
{
try
{
DataTable dtLP = GetLPs(strSort);
DataTable dt = new DataTable("LP_Add");

DateTime dte = System.DateTime.Now;
dt = dtLP.Clone();

DataRow dr = dt.NewRow();
dr["PostDate"] = dte.ToShortDateString();
dr["Status"] = "Pending-Active";
dt.Rows.Add(dr);
this.dlLPDetail.DataSource = dt;
this.dlLPDetail.EditItemIndex = 0;
this.dlLPDetail.DataBind();
dsplyDataListItem();
ViewState["Update"] = "Add";
//vs = "Add";
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Repsonse.Write("<br>addLP");
}
}


#region Display Properties
private void dsplyDataGrid()
{
this.btnAddBot.Visible = true;
this.btnAddTop.Visible = true;
this.LPDataGrid.Visible = true;
this.dlLPDetail.Visible = false;
}

private void dsplyDataListItem()
{
this.btnAddBot.Visible = false;
this.btnAddTop.Visible = false;
this.LPDataGrid.Visible = false;
this.dlLPDetail.Visible = true;
}
#endregion

#region DropDownList Data
protected int GetSelIndex(string stat)
{
try
{
DataTable dt = new DataTable();
dt = dsLP.Tables["Statuses"];
DropDownList ddl = new DropDownList();

for(int i=0;i < dt.DefaultView.Count;i++)
{
if((string)dt.DefaultView["Status"]==stat)
{
return i;
}
}
return 0;
}
catch
{
Response.Write("There was an error rendering this page. Please contact
your System Administrator.");
Response.Write("<br>GetSelIndex");
return 0;
}
}

public DataTable GetStatuses()
{
SqlCommand sqlStatComm = new SqlCommand("sp_Select_Status", sqlConn);
SqlDataAdapter statAdapter = new SqlDataAdapter(sqlStatComm);
DataTable dt = new DataTable();

statAdapter.Fill(dsLP, "Statuses");
dt = dsLP.Tables["Statuses"];

return dt;
}
#endregion

#region Grid OnClick Events

public void DoItemDetails(object s, DataGridCommandEventArgs e)
{

}

public void DoItemDelete(object objSource, DataGridCommandEventArgs e)
{
//Get the ID_LP from the row in the data grid
string idLP = LPDataGrid.DataKeys[e.Item.ItemIndex].ToString();

//Delete the record from the database
strSqlComm = "sp_LP_Delete";
SqlCommand command = new SqlCommand(strSqlComm, sqlConn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@idLP", SqlDbType.Int).Value = idLP;
command.Parameters.Add("@RtnCode", ParameterDirection.Output);

sqlConn.ConnectionString = strSqlConn;

sqlConn.Open();
command.ExecuteNonQuery();
sqlConn.Close();

LPDataGrid.EditItemIndex = -1;

Response.Write("Record has been successfully deleted.");

//Rebind the datagrid
BindData(strSort);
}

public void DoItemSort(Object o, DataGridSortCommandEventArgs e)
{
string[] sortExp;
char[] delim = {' '};
sortExp = e.SortExpression.Split(delim);
string strField = sortExp[0];
string strOrder = string.Empty;
string newSort = string.Empty;
int indx = int.MinValue;

if (sortExp.Length > 1)
{
strOrder = sortExp[1];
switch (strOrder)
{
case "ASC":
strOrder = "DESC";
break;
case "DESC":
strOrder = "ASC";
break;
}
}
else strOrder = "ASC";

newSort = strField + " " + strOrder;

switch (strField)
{
case "Title":
indx = 4;
break;
case "PostDate":
indx = 6;
break;
case "DueDate":
indx = 7;
break;
}

this.LPDataGrid.Columns[indx].SortExpression = newSort;

BindData(newSort);
}

public void DoItemEdit(Object objSource, DataGridCommandEventArgs e)
{
//this.dlLPDetail.EditItemIndex = -1;
dsplyDataListItem();
string id = LPDataGrid.DataKeys[e.Item.ItemIndex].ToString();
int idLP = Int32.Parse(id);
this.dlLPDetail.EditItemIndex = e.Item.ItemIndex;
BindDetailData(idLP);
//this.dlLPDetail.EditItemIndex = e.Item.ItemIndex;
Response.Write((string)dlLPDetail.EditItemIndex.ToString());
ViewState["Update"] = "Edit";
//vs = "Edit";
}

#endregion

#region Detail DataList OnClick Events
public void DoDetItem(object s, DataListCommandEventArgs e)
{
//imgCalPD;
}

public void DoItemCancel(Object objSource, DataListCommandEventArgs e)
{
LPDataGrid.EditItemIndex = -1;
dlLPDetail.EditItemIndex = -1;
BindData(strSort);
dsplyDataGrid();
}
public void DoItemUpdate(Object objSource, DataListCommandEventArgs e)
{
string vs = (string)ViewState["Update"];
string idLP = ((TextBox)e.Item.FindControl("tbID")).Text;
int id = 0;
if (idLP != "")
{
id = Int32.Parse(idLP);
}
string title = ((TextBox)e.Item.FindControl("tbTitle")).Text;
string body = ((TextBox)e.Item.FindControl("tbBody")).Text;
string stat =
((DropDownList)e.Item.FindControl("lstStatus")).SelectedItem.Value;
string pdt = ((TextBox)e.Item.FindControl("tbPostDate")).Text;
string ddt = ((TextBox)e.Item.FindControl("tbDueDate")).Text;
switch(vs)
{
case "Edit":
strSqlComm = "sp_LP_Update";
break;
case "Add":
strSqlComm = "sp_LP_Insert";
break;
}

SqlCommand command = new SqlCommand(strSqlComm, sqlConn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@title", SqlDbType.NVarChar, 200).Value =
title;
command.Parameters.Add("@body", SqlDbType.NVarChar, 1000).Value = body;
command.Parameters.Add("@stat", SqlDbType.Int).Value = stat;
command.Parameters.Add("@pDt", SqlDbType.DateTime).Value = pdt;
command.Parameters.Add("@dDt", SqlDbType.DateTime).Value = ddt;
if (vs == "Edit")
{
command.Parameters.Add("@idLP", SqlDbType.Int).Value = id;
}
command.Parameters.Add("@RtnCode", ParameterDirection.Output);

sqlConn.ConnectionString = strSqlConn;

sqlConn.Open();
command.ExecuteNonQuery();
sqlConn.Close();

dlLPDetail.EditItemIndex = -1;
//BindDetailData(id);

LPDataGrid.EditItemIndex = -1;
BindData(strSort);
dsplyDataGrid();
ViewState["Update"] = String.Empty;
}
#endregion

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
//InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
 
D

Dave Jansen

I was able to resolve the issue.

The DataGrid contained many records while the DataList only contains one
(the one I'm currently editing).

Hence the EditItemIndex of the DataList has to be 0.

Thanks again for all the input.

Dave
 

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

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top