2013-10-08 49 views
1

家長的隱藏字段存儲的值我有一個父數據列表(DataList1)和一個孩子DataList控件(的childList)。有沒有辦法將DataList1(parent)中的Value(Company)字段存儲在子DataList(childList)中的隱藏字段(hiddenCompanyFromParent)中,以便稍後處理?ASP.NET - 嵌套DataList控件,從孩子的DataList

請幫助....

<asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyListPartialMatch" runat="server" Width="80%" DataKeyField="Company1Word" 
    UseAccessibleHeader="true" 
    CssClass="books" 
    HeaderStyle-CssClass="header" 
    ItemStyle-CssClass="item" 
    AlternatingItemStyle-CssClass="alternating" 
    GridLines="Both" 
    CellPadding="0" 
    CellSpacing="0" BorderColor="Black" 
    ItemStyle-BorderColor="Black" BorderWidth="0" 
    HorizontalAlign="Center" 
    RepeatDirection="Vertical" 
    >     
    <HeaderTemplate> 
     <table border="0" width="100%"> 
     <tr class="div_hover"> 
      <th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; "></th> 
      <th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">Num</th> 
      <th style="width: 70%; border-right:1px solid black; border-spacing:0; text-align:center; ">Company Name</th> 
      <th style="width: 10%; border-right:1px solid black; border-spacing:0; text-align:center; ">Add?</th> 
     </tr> 
     </table> 
    </HeaderTemplate> 

    <ItemStyle BorderColor="black" Font-Size="Medium" /> 

    <ItemTemplate> 
     <table border="0" width="100%"> 
     <tr class="div_hover"> 
      <td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">   
      <asp:LinkButton> 
       <asp:LinkButton ID="LinkButton2" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>' 
       OnCommand="LinkButton1_Command" 
       Font-Underline="false"  
       Height="25"  
       Font-Bold="true"     
       ></asp:LinkButton> 
      </td> 
      <td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:right; padding-right:10px;"><%#Eval("Row")%></td> 
      <td style="width: 70%"><asp:Literal ID="ltlCompany" runat="server" Text='<%#Eval("Company")%>' /> </td> 

      <asp:Label ID="lblRow" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "Row") %>' /> 
     </tr>  
     </table>  
      <asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;"> 
       <asp:DataList ID="childList" runat="server" Width="100%"> 
        <ItemTemplate> 
         <div class="div_hover"> 
         <table class="table1" width="80%"> 
         <tr> 
          <td style="width: 60%; border-right:0px solid black; border-spacing:0;">&#8226; <%#Eval("CompanyName")%></td> 
          <td style="width: 20%;text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td> 

          <td style="width: 20%;text-align:right;"><asp:CheckBox id="chkChildCompany" runat="server" value="123Test" 
           AutoPostBack="true" 
           OnCheckedChanged="chkChildCompany_CheckedChanged" CustomAttribute='<%#Eval("CompanyID") %>' /></td> 
          <asp:Label ID="hidden" Visible="True" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "CompanyID") %>' /> 
          <asp:HiddenField ID="hiddenCompanyFromParent" runat="server" Value='<%#Eval("Company FROM PARENT DATALIST1") %>' />      
         </tr> 
         </table> 
         </div> 
        </ItemTemplate> 
       </asp:DataList> 
      </asp:Panel> 
    </ItemTemplate> 

    <FooterTemplate> 
    </FooterTemplate>     
</asp:DataList> 

我試試下面這段代碼,但它不工作要麼。

<asp:HiddenField ID="hiddenCompanyParent" runat="server" value='<%# DataBinder.Eval(Container.NamingContainer.NamingContainer, "DataItem.Company")%>' /> 

回答

1

您的隱藏字段深入DataList - > Panel - > Child DataList。在這種情況下,你必須深入挖掘:

<asp:HiddenField ID="hiddenCompanyFromParent" runat="server" Value='<%# DataBinder.Eval(Container.Parent.Parent.Parent, "DataItem.Company")%> ' /> 

編輯:你重新綁定在鏈接按鈕的Click事件DataList的孩子,所以回傳後父DataList的數據不可用。你可以做的一件事是在孩子Datalist外面添加一個隱藏的字段。這裏我在面板內添加:

<asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;"> 
     <asp:HiddenField ID="hdnCompany" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "Company")%> ' /> 

現在在代碼中,您可以找到隱藏字段的值並使用它。即使您可以將此值分配給子DataList中的隱藏字段:

protected void childList_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     HiddenField hdnCompany = (HiddenField)e.Item.NamingContainer.Parent.FindControl("hdnCompany"); 
     HiddenField hdnChildCompany = (HiddenField)e.Item.FindControl("hiddenCompanyFromParent"); 

     if (hdnCompany != null && hdnChildCompany != null) 
     { 
      hdnChildCompany.Value = hdnCompany.Value; 
     } 
    } 
} 

並使用來自任何隱藏字段的值。

+0

是的,你是正確的。但是有一個問題,它會重置那個hiddenField。我有DataList1的「按需=」 LinkBut​​ton1_Command」,這將得到的childList下的企業名單和地點,然後childList.Bind()結尾事件保持復位的childList,所以在hiddenField這些值都沒有了。任何建議? – milacay

+1

然後,我會建議設置在childList_ItemDataBound方法隱藏字段的值。 – afzalulh

+0

任何代碼的例子嗎?謝謝! – milacay