循环遍历表单上的控件始终获得第一个控件

发布于 2024-12-01 15:53:50 字数 3688 浏览 4 评论 0原文

我有一个网络表单,可以在其中动态加载网络用户控件。 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);
            }
        }
    }
}

编辑 对于中继器,此隐藏字段评级仅在第一个重复控件上设置,以下是我的 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>

如何循环遍历各个级别的中继器控件并获取此设置中的中继器项目?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在我安 2024-12-08 15:53:50

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

首先将此行

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

更改为

if ( ctl is RepeaterItem )

因为你拥有的很丑陋。

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

You aren't going deep enough into the child controls.

Firstly change this line

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

to this

if ( ctl is RepeaterItem )

as what you have is ugly.

then make a change to GetRatingControl to recursively call itself for each control that is a RepeaterItem.

來不及說愛妳 2024-12-08 15:53:50

请检查这个问题并回答。

更改检查中继器的条件。

递归检查页面内的 ListBox

please check this question and answer.

Change the condtion to check for a Repeater.

Check for ListBox inside a page recursively

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文