我正在尝试将ListView从一个类复制到另一个类,并在新的aspx页面上显示ListView。我正在做的是我从第一个ListView中选择某些值,只有那些项目才会显示在新页面上。但是,我无法弄清楚如何在新页面上显示ListView,因为我习惯使用SqlDataSource。我通过addList方法传递ListView,我已经在新的aspx文件中创建了一个新的ListView。 如果我需要添加更多信息,请告诉我处理此问题的最佳方法。
头等舱的代码
public void CloneButton_Click(object sender, EventArgs e)
{
String droplist = DropDownList2.SelectedValue;
list = new ListView();
if (String.IsNullOrEmpty(droplist))
{
ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Please select a Program Name and Report Period');", true);
return;
}
else
{
if (ListView1.Items.Count == 0)
{
Button1_Click(sender, e);
}
else
{
var selectAllCheckBox = (CheckBox)ListView1.InsertItem.FindControl("CheckBox2");
if (selectAllCheckBox.Checked == true)
{
list = ListView1;
Response.Redirect("~/CloneReport.aspx");
return;
}
for (int i = 0; i < ListView1.Items.Count; i++)
{
CheckBox chk = (CheckBox)ListView1.Items[i].FindControl("CheckBox1");
if (chk.Checked == true)
{
list.Items.Add(ListView1.Items[i]);
}
}
CloneReport rep = new CloneReport();
rep.addList(list);
Response.Redirect("~/CloneReport.aspx");
}
}
}
第二类代码
public void addList(ListView list)
{
ListView1 = new ListView();
for (int i = 0; i < list.Items.Count; i++)
{
ListView1.Items.Add(list.Items[i]);
}
ListView1.DataBind();
}
旧页面的ListView项目模板(从页面转移)
<ItemTemplate>
<tr style="background-color: #E0FFFF; color: #333333;">
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
</td>
<td>
<asp:Label ID="FormTitleLabel" runat="server" Text='<%# Eval("FormTitle") %>' />
</td>
<td>
<asp:Label ID="FormSectionLabel" runat="server" Text='<%# Eval("FormSection") %>' />
</td>
<td>
<asp:Label ID="SubSectionLabel" runat="server" Text='<%# Eval("SubSection") %>' />
</td>
<td>
<asp:Label ID="SectionItemLabel" runat="server" Text='<%# Eval("SectionItem") %>' />
</td>
<td>
<asp:Label ID="SortOrder" runat="server" Text='<%# Eval("SortOrder") %>' />
</td>
<td>
<asp:Label ID="SectionSortOrder" runat="server" Text='<%# Eval("SectionSortOrder") %>' />
</td>
<td>
<asp:Label ID="SubSectionSortOrder" runat="server" Text='<%# Eval("SubSectionSortOrder") %>' />
</td>
<td>
<asp:Label ID="RuleDesc" runat="server" Text='<%# Eval("RuleDesc") %>' />
</td>
<td>
<asp:Label ID="ControlType" runat="server" Text='<%# Eval("ControlType") %>' />
</td>
<td>
<asp:Label ID="CrossItem" runat="server" Text='<%# Eval("CrossItem") %>' />
</td>
</tr>
</ItemTemplate>
更新面板的触发器
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" />
<asp:AsyncPostBackTrigger ControlID="Button1" />
<asp:AsyncPostBackTrigger ControlID="CloneButton" />
</Triggers>
<ContentTemplate>
<div style="text-align: center;">
<asp:Button ID="CloneButton" runat="server" Text="Clone Report Period" OnClick="CloneButton_Click" />
</div>
<div style="text-align: center; position: absolute; margin-left: auto; margin-right: auto; left: 0; right: 0">
<b>Program Name</b>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Program" DataValueField="ProgramID">
</asp:DropDownList>
   
<b>Report Period</b>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="ReportLabel" DataValueField="DataCollectionPeriodID" Height="21px" Width="172px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Height="30px" OnClick="Button1_Click" Text="Search" />
</div>
答案 0 :(得分:1)
您无法在页面之间传输ListView对象,但可以使用Server.Transfer使用DataTable进行传输。
第一页:
DataTable dtTable;
public DataTable DataTransferTable
{
get { return dtTable; }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lstTransferView = new ListView();
DataView dtView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
dtTable = new DataTable();
dtTable = dtView.ToTable().Clone();
DataRow dtRow;
foreach (ListViewDataItem lstItem in lstView.Items)
{
if (((CheckBox)lstItem.FindControl("chkBox")).Checked)
{
dtRow = (DataRow)dtView.Table.Rows[lstItem.DataItemIndex];
dtTable.ImportRow(dtRow);
}
}
Server.Transfer("~/SecondPage.aspx");
}
第二页:
public FirstPageClass ftPage;
DataTable dtNewTable;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
ftPage= (FirstPageClass)Context.Handler;
dtNewTable = (DataTable)ftPage.DataTransferTable;
lstSecondView.DataSource = dtNewTable;
lstSecondView.DataBind();
}