1

How do I copy an .aspx ListView (including it's LayoutTemplate and ItemTemplate) from the aspx.cs file? The .aspx.cs uses DataBind() to display the values onto the page.

so essentially I want to turn this:

<asp:ListView ID="EvalAnswerList" OnItemDataBound="EvalAnswerList_ItemDataBound" runat="server">
            <LayoutTemplate>
                <table cellpadding="2" width="640px" border="1" runat="server" class="altRows" id="tblProducts">
                    <tr id="Tr1" runat="server">
                        <th id="Th1" runat="server">Question
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr id="Tr2" runat="server">
                    <td>
                        <asp:Label ID="QuestionTextLabel" Font-Bold="true" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
</asp:ListView>

Into this:

<asp:ListView ID="EvalAnswerList" OnItemDataBound="EvalAnswerList_ItemDataBound" runat="server">
            <LayoutTemplate>
                <table cellpadding="2" width="640px" border="1" runat="server" class="altRows" id="tblProducts">
                    <tr id="Tr1" runat="server">
                        <th id="Th1" runat="server">Question
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr id="Tr2" runat="server">
                    <td>
                        <asp:Label ID="QuestionTextLabel" Font-Bold="true" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
</asp:ListView>
<asp:ListView ID="EvalAnswerList" OnItemDataBound="EvalAnswerList_ItemDataBound" runat="server">
            <LayoutTemplate>
                <table cellpadding="2" width="640px" border="1" runat="server" class="altRows" id="tblProducts">
                    <tr id="Tr1" runat="server">
                        <th id="Th1" runat="server">Question
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr id="Tr2" runat="server">
                    <td>
                        <asp:Label ID="QuestionTextLabel" Font-Bold="true" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
</asp:ListView>

This copying has to be done in the aspx.cs file. Obviously it won't be exactly like this but I wanted to give you an idea of what I am trying to do. I've tried saying:

ListView test = new ListView();
PlaceHolder.Controls.Add(test);
test = EvalAnswerList;

and then trying to use this in the test.DataBind() but it won't work.

4

2 回答 2

1

您可以使用 webusercontrol,尝试以下步骤

第 1 步:创建一个用户控件并将列表视图放入其中

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomListView.ascx.cs"
    Inherits="CustomListView" %>
<asp:ListView ID="EvalAnswerList" OnItemDataBound="EvalAnswerList_ItemDataBound"
    runat="server">
    <layouttemplate>
                <table cellpadding="2" width="640px" border="1" runat="server" class="altRows" id="tblProducts">
                    <tr id="Tr1" runat="server">
                        <th id="Th1" runat="server">Question
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder" />
                </table>
            </layouttemplate>
    <itemtemplate>
                <tr id="Tr2" runat="server">
                    <td>
                        <asp:Label ID="QuestionTextLabel" Font-Bold="true" runat="server" />
                    </td>
                </tr>
            </itemtemplate>
</asp:ListView>
enter code here

Setp 2:创建一个公共 DataSet/DataTable 以在页面加载中绑定列表视图

    public DataSet dsSource = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EvalAnswerList.DataSource = dsSource;
            EvalAnswerList.DataBind();
        }
    }
enter code here

第 3 步:现在在您的 aspx 页面中添加对 usercontrol 的引用

<%@ Reference Control="CustomListView.ascx" %>
enter code here

第 4 步:最后,现在您可以在 aspx 中多次使用该列表视图。取一个面板或 div,然后将用户控件添加到您想要的位置。

CustomListView clv = new CustomListView();
clv.dsSource = ds;//dataset/datatable to bind listview
div.Controls.Add(clv);

祝你好运!

于 2013-06-28T10:37:28.203 回答
0

最后,我使用了中继器控件并且它起作用了。谢谢!

于 2013-09-12T19:01:29.493 回答