我有一个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);
}
}
}
}
修改 的 对于转发器,此隐藏字段评级仅在第一个重复控件上进行设置,以下是我的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>
如何在不同级别的转发器控制中循环并在此设置中获取转发器项目?
答案 0 :(得分:3)
你没有深入到儿童控制中。
首先更改此行
if ( ctl.GetType().ToString() == "System.Web.UI.WebControls.RepeaterItem" )
到这个
if ( ctl is RepeaterItem )
然后对GetRatingControl进行更改,以便为每个作为RepeaterItem的控件递归调用自身。
答案 1 :(得分:0)