2011-08-28 52 views
0

我有一個webform,它動態加載一個web用戶控件。在Web用戶控件內部是一箇中繼器控件,並且可以根據需要在其中自動創建Web用戶控件,並將所有這些控件加載到佔位符中。循環通過窗體上的控件始終獲得第一個控件

如:

webusercontrol1 
    repeater 
    webusercontrol 
     repeater 
    webusercontrol 
     repeater 
     webusercontrol 
     repeater 

當我遍歷佔位符中的控件,在出現的唯一的中繼器是第一個中繼器。

我的代碼如下:

protected void cmdsave_Click(object sender, EventArgs e) 
{ 
    foreach (Control ctl in this.officephld.Controls) 
    { 
     if (ctl.GetType().ToString() == "ASP.evalctl_ascx") 
     { 
      foreach (Control sctl in ctl.Controls) 
      { GetRatingControl(sctl); } 
     } 
    } 
    Response.Redirect("~/contractoreval.aspx?sc=1"); 
} 
protected void GetRatingControl(Control item) 
{ 
    Repeater rpt = (Repeater)item.FindControl("rptPts"); 
    foreach (Control ctl in rpt.Controls) 
    { 
     if (ctl.GetType().ToString() == "System.Web.UI.WebControls.RepeaterItem") 
     { 
      RepeaterItem ri = (RepeaterItem)ctl; 
      HiddenField pntid = (HiddenField)ri.FindControl("pntid"); 
      HiddenField catid = (HiddenField)ri.FindControl("catid"); 
      HiddenField rating = (HiddenField)ri.FindControl("rating"); 
      if (pntid != null && catid != null) 
      { 
       AjaxControlToolkit.Rating rtg = (AjaxControlToolkit.Rating)ri.FindControl("pntrating"); 
       SQLConnectivity db = new SQLConnectivity(); 
       SqlParameter[] param = new SqlParameter[8]; 
       int iRetValue = 0; 

       param[0] = db.MakeInputParameter("@eval_id", Convert.ToInt32(pntid.Value)); 
       param[1] = db.MakeInputParameter("@category_id", Convert.ToInt32(catid.Value)); 
       param[2] = db.MakeInputParameter("@organization_id", 1); 
       param[3] = db.MakeInputParameter("@subcontractor_id", 1); 
       param[4] = db.MakeInputParameter("@project_id", 1); 
       param[5] = db.MakeInputParameter("@rating", Convert.ToInt32(rating.Value)); 
       param[6] = db.MakeInputParameter("@created_by", 1); 
       param[7] = db.MakeInputParameter("@updated_by", 1); 
       db.RunNonQueryProcedure("PerformanceSubcontractorEvalSave", param, ref iRetValue); 
      } 
     } 
    } 
} 

編輯 該轉發器,這hiddenfield評級僅被instatiated第一重複控制,下面是我的HTML標記:

<asp:Repeater ID="rptPts" runat="server" Visible="false" 
    onitemdatabound="rptPts_ItemDataBound"> 
    <HeaderTemplate></HeaderTemplate> 
    <ItemTemplate> 
     <div style="width:75%;float:left;padding-top:3px;height:20px;"> 
      <asp:Label runat="server" ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem, "eval_description") %>' Font-Names="Arial" Font-Size="10px"></asp:Label> 
     </div> 
     <asp:HiddenField ID="catid" runat="server" /> 
     <asp:HiddenField ID="pntid" runat="server" /> 
     <asp:HiddenField ID="rating" runat="server" /> 
     <div style="width:20%;float:right;padding-top:3px;padding-bottom:3px;height:20px;"> 
      <cc1:Rating ID="pntrating" runat="server" FilledStarCssClass="filldStar" OnChanged="pntrating_Changed" EmptyStarCssClass="emptyStar" StarCssClass="filldStar" WaitingStarCssClass="savedStar" EnableViewState="true" AutoPostBack="false" BehaviorID="ratingControlBehavior"> 
      </cc1:Rating> 
     </div> 
     <div style="clear:both;"></div> 
    </ItemTemplate> 
    <FooterTemplate></FooterTemplate> 
</asp:Repeater> 

我該如何循環通過中繼器控制的各個級別並獲得此設置中的中繼器項目?

回答

3

你沒有深入到子控件中。

此行

if (ctl.GetType().ToString() == "System.Web.UI.WebControls.RepeaterItem")

首先改變這種

if (ctl is RepeaterItem)

,你有什麼是醜陋的。

然後對GetRatingControl進行更改,以針對每個RepeaterItem控件遞歸調用自身。

+0

我在想這樣做遞歸,但我被陷在何處執行遞歸。 – mattgcon

+0

在foreach循環中。 – kmcc049

+0

好吧,現在確實是在那個循環內部的IF語句中還是在它之後呢? – mattgcon

0

請檢查這個問題和答案。

更改條件以檢查Repeater。

Check for ListBox inside a page recursively

+0

我看不出這個問題和答案能夠真正幫助我,因爲它與我的問題 – mattgcon

+0

@mattgcon無關。你只是檢查窗體上的控件。我提到的鏈接是檢查一個列表框。你正在檢查一個Repeater。控制類型是不同的。兩個例子中使用的邏輯都是一樣的。 –

相關問題