简体   繁体   中英

How to copy an aspx ListView from the aspx.cs file?

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.

You can use a webusercontrol, try below steps

Step 1: Create a usercontrol and put your list view in that

<%@ 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: Create a public DataSet/DataTable to bind listview in page load

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

Step 3: Now add reference to usercontrol in your aspx page

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

Step 4: At last, now you can use that list view multiple times in you aspx. Take a panel or div, then add usercontrol to where you want.

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

gud luck!

In the end, I used a repeater control and it worked. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM