0

I have a webform which dynamically loads a web user control in it. Within the web user control is a repeater control, and the web user control can be dynamically created within it self as many times as needed, all of these are loaded into a placeholder.

eg:

webusercontrol1
   repeater
   webusercontrol
      repeater
   webusercontrol
      repeater
      webusercontrol
         repeater

When I loop through the controls within the placeholder, the only repeater that comes up is the first repeater.

my code is as follows:

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);
            }
        }
    }
}

EDIT for the repeater, this hiddenfield rating is only being instatiated on the first repeat control, the following is my HTML markup:

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

how can I loop through the various levels of repeater control and get the repeater items within this setup?

4

2 回答 2

3

您对子控件的了解不够深入。

首先改变这一行

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

对此

if ( ctl is RepeaterItem )

因为你所拥有的是丑陋的。

然后对 GetRatingControl 进行更改,以便为每个作为 RepeaterItem 的控件递归调用自身。

于 2011-08-28T08:32:16.743 回答
0

请检查这个问题和答案。

更改条件以检查中继器。

递归检查页面内的 ListBox

于 2011-08-28T08:49:55.393 回答