如何从aspx.cs文件中复制aspx ListView?

时间:2013-06-28 00:26:54

标签: c# asp.net .net

如何从aspx.cs文件中复制.aspx ListView(包括它的LayoutTemplate和ItemTemplate)? .aspx.cs使用DataBind()在页面上显示值。

基本上我想转此:

<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>
<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>

此复制必须在aspx.cs文件中完成。显然它不会完全是这样,但我想让你知道我想要做什么。 我试过说:

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

然后尝试在test.DataBind()中使用它,但它不起作用。

2 个答案:

答案 0 :(得分: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以在页面加载中绑定listview

    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,然后将usercontrol添加到你想要的位置。

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

gud luck!

答案 1 :(得分:0)

最后,我使用了一个转发器控件,它起作用了。谢谢!