我有一个 Asp:ListView 控件,我在 Web 表单的弹出窗口中显示该控件。目前,只要我单击插入、更新或删除链接按钮,它就会直接在数据库中进行所有更改。
但是只有在提交 Web 表单时,我才需要在数据库中完成所有更改。
所以,我想我需要在会话中保留绑定到 ListView 的数据,并且在所有插入、更新和删除操作中,通过
步骤 1)手动对 OnInserting、OnDeleting、OnUpdating 事件进行所有更改)检索每次来自 Session["ListView1"] 的数据,
步骤 2) 对检索到的数据执行插入/删除/更新操作
步骤 3) 将其绑定到 ListView
步骤 4) 更新后替换 Session["ListView1"] 中的数据。
第 5 步)在 Web 表单提交时,获取更新的数据并在数据库中全部替换?
我做了这些步骤,但它没有按预期工作。在更新时,ListView 显示 2 列的空白值并错误地绑定数据。我已经绑定了正确的列。插入后,TextBox 没有按预期更改为 Labels。下面是我的 ListView 和代码隐藏代码:
列表显示:
<asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server"
OnItemCanceling="ListView1_ItemCanceling" OnItemEditing="ListView1_ItemEditing"
OnItemDeleting="ListView1_ItemDeleting" OnItemUpdating="ListView1_ItemUpdating"
OnItemInserting="ListView1_ItemInserting" DataKeyNames="Id"
InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<tr style="">
<td width="200">
<asp:LinkButton ClientIDMode="AutoID" ID="DeleteButton" runat="server"
CommandName="Delete" Text="Delete" CausesValidation="false" />
<asp:LinkButton ClientIDMode="AutoID" ID="EditButton" runat="server"
CommandName="Edit" Text="Edit" CausesValidation="false" />
</td>
<td>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="Column3Label" runat="server" Text='<%# Eval("Column3") %>' />
</td>
<td>
<asp:Label ID="Column4Label" runat="server" Text='<%# Eval("Column4") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:LinkButton ClientIDMode="AutoID" ID="UpdateButton" runat="server"
CommandName="Update" Text="Update" ValidationGroup="popup1" CausesValidation="true" />
<asp:LinkButton ClientIDMode="AutoID" ID="CancelButton" runat="server"
CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</td>
<td>
<asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:TextBox ID="FirstNameTextBox" Style="width: 100px; background-color: white;"
runat="server" Text='<%# Bind("FirstName") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="NameReq" runat="server" ControlToValidate="FirstNameTextBox"
ErrorMessage="First name is required." ToolTip="First name is required."
ValidationGroup="popup1">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="LastNameTextBox" Style="width: 100px; background-color: white;"
runat="server" Text='<%# Bind("LastName") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="LastNameTextBox" ErrorMessage="Last name is required."
ToolTip="Last name is required." ValidationGroup="popup1">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="Column3TextBox" Style="width: 100px; background-color: white;"
runat="server" Text='<%# Bind("Column3") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="Column3TextBox" ErrorMessage="ID is required." ToolTip="ID is required."
ValidationGroup="popup1">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="Column4Text" placeholder="mm/dd/yyyy" Style="width: 100px;
background-color: white;" runat="server" Text='<%# Bind("Column4") %>'
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="Column4Text" ErrorMessage="Date is required."
ToolTip="Date is required." ValidationGroup="popup1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:RegularExpressionValidator ID="dateValRegex" runat="server"
ControlToValidate="Column4Text" ErrorMessage="Please Enter a valid date in the format (mm/dd/yyyy)"
ValidationExpression="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
ValidationGroup="popup"></asp:RegularExpressionValidator>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr>
<td width="200">
<asp:LinkButton ClientIDMode="AutoID" ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" ValidationGroup="popup" CausesValidation="true" />
<asp:LinkButton ClientIDMode="AutoID" ID="LinkButton1" runat="server" CommandName="Cancel"
Text="Clear" CausesValidation="false" />
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>' />
</td>
<td>
<asp:TextBox ID="TextBox1" Style="width: 100px; background-color: white;" runat="server"
Text='<%# Bind("FirstName") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="FirstNameTextBox" ErrorMessage="First name is required."
ToolTip="First name is required." ValidationGroup="popup">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="TextBox2" Style="width: 100px; background-color: white;" runat="server"
Text='<%# Bind("LastName") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="LastNameTextBox" ErrorMessage="Last name is required."
ToolTip="Last name is required." ValidationGroup="popup">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="TextBox3" Style="width: 100px; background-color: white;" runat="server"
Text='<%# Bind("Column3") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="Column3TextBox" ErrorMessage="ID is required."
ToolTip="ID is required." ValidationGroup="popup">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="TextBox4" placeholder="mm/dd/yyyy" Style="width: 100px;
background-color: white;" runat="server" Text='<%# Bind("Column4") %>' CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Column4Text"
ErrorMessage="Date is required." ToolTip="Date is required."
ValidationGroup="popup">*</asp:RequiredFieldValidator>
</tr>
<tr>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="Column4Text" ErrorMessage="Please Enter a valid date in the format (mm/dd/yyyy)"
ValidationExpression="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
ValidationGroup="popup"></asp:RegularExpressionValidator>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr>
<td width="200">
<asp:LinkButton ClientIDMode="AutoID" ID="LinkButton2" runat="server" CommandName="Delete"
Text="Delete" CausesValidation="false" />
<asp:LinkButton ClientIDMode="AutoID" ID="LinkButton3" runat="server" CommandName="Edit"
Text="Edit" CausesValidation="false" />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Column3") %>' />
</td>
<td>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Column4") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">Action</th>
<th id="Th2" runat="server">Id</th>
<th id="Th3" runat="server">First Name</th>
<th id="Th4" runat="server">Last Name</th>
<th id="Th5" runat="server">Column3 Name</th>
<th id="Th6" runat="server">Column4 Name</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
<table width="900" align="center" style="text-align: center;">
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete" Text="Delete"
CausesValidation="false" />
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="Edit" Text="Edit"
CausesValidation="false" />
</td>
<td>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("Column3") %>' />
</td>
<td>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("Column4") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:updatepanel>
下面是代码背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
LoadListView();
}
}
private void LoadListView()
{
try
{
List lstDetails = Session["ListView1Data"] as List;
ListView1.DataSource = lstDetails;
ListView1.DataBind();
Session["ListView1Data"] = lstDetails ;
}
catch (Exception ex)
{
ExceptionLogger.LogException(ex);
}
}
protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
List lstDetails = Session["ListView1Data"] as List;
int id = Convert.ToInt32(e.Values["Id"]);
if (lstDetails.Exists(x => x.Id == id))
{
var tableDetail = lstDetails.Where(x => x.Id == id).FirstOrDefault();
lstDetails.Remove(tableDetail);
hdnListDeleteIds.Value = id + "|";
ListView1.DataSource = lstDetails;
ListView1.DataBind();
ListView1.EditIndex = -1;
}
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
List lstDetails = Session["ListView1Data"] as List;
int id = Convert.ToInt32(e.OldValues["Id"]);
if (lstDetails.Exists(x => x.Id == id))
{
var tableDetail = lstDetails.Where(x => x.Id == id).FirstOrDefault();
tableDetail.FirstName = e.NewValues["FirstName"].ToString();
tableDetail.LastName = e.NewValues["LastName"].ToString();
tableDetail.Column3 = e.NewValues["Column3"].ToString();
tableDetail.Column4 = Convert.ToDateTime(e.NewValues["Column4"]);
hdnListUpdateIds.Value = id + "|";
ListView1.DataSource = lstDetails;
ListView1.DataBind();
ListView1.EditIndex = -1;
Session["ListView1Data"] = lstDetails;
}
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
List lstDetails = Session["ListView1Data"] as List;
EntityFrameworkModel.TableDetails tableDetail = new EntityFrameworkModel.TableDetails()
{
FirstName = e.Values["FirstName"].ToString(),
LastName = e.Values["LastName"].ToString(),
Column3 = e.Values["Column3"].ToString(),
Column4 = Convert.ToDateTime(e.Values["Column4"]),
Id = 0
};
lstDetails.Add(tableDetail);
Session["ListView1Data"] = lstDetails;
ListView1.DataSource = lstDetails;
ListView1.DataBind();
ListView1.EditIndex = -1;
}
protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
List lstDetails = Session["ListView1Data"] as List;
ListView1.DataSource = lstDetails;
ListView1.DataBind();
e.Cancel = true;
ListView1.EditIndex = -1;
}
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
}
我究竟做错了什么?请帮忙。
非常感谢