2013-06-28 43 views
1

如何從aspx.cs文件複製.aspx ListView(包括LayoutTemplate和ItemTemplate)? .aspx.cs使用DataBind()將值顯示到頁面上。如何從aspx.cs文件複製aspx 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> 
<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()來使用這個,但它不會工作。

+1

如何使用中繼器控制? –

回答

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中綁定列表視圖中頁面加載

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

到用戶控件0
<%@ 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!

相關問題