转发器内的用户控件 - 回发后没有更改
我在中继器中有自己的用户控件。它根据列表元素评级显示星级。在第一次绑定时一切正常,每个项目的星星数量都可以。然后我进行回发并过滤此列表。然后将结果数据绑定到中继器。假设一开始我有 5 个元素,过滤后只有 3 个元素,但只有 3 个元素可见,但星星的顺序与绑定前一样。
我想要的是在回发期间应重新创建中继器内的每个控件,并应显示适当数量的星星(而不是旧的星星)。
我做错了什么?
中继器位于 updatePanel 内。
这是我的控件“星星”代码:
<span runat="server" id="starHolder" class="starsHolder" title="">
<input runat="server" id="star1" name="star3" type="radio" class="star1" value="1" />
<input runat="server" id="star2" name="star3" type="radio" class="star1" value="2" />
<input runat="server" id="star3" name="star3" type="radio" class="star1" value="3" />
<input runat="server" id="star4" name="star3" type="radio" class="star1" value="4"/>
<input runat="server" id="star5" name="star3" type="radio" class="star1" value="5"/>
</span>
这是我的转发器代码:
<asp:Repeater EnableViewState="false" OnItemDataBound="RptrTopRated_OnDataItemBound" ID="rptrTopRated" runat="server">
<ItemTemplate>
<li class="topRated" id='<%#DataBinder.Eval(Container.DataItem, "[id]")%>'>
<p>
<%# DataBinder.Eval(Container.DataItem, "[title]") %>
</p>
<span class="ratingStar">
<cms:stars EnableViewState="false" runat="server" id="ctrlRating" McID='<%#System.Convert.ToInt32(DataBinder.Eval(Container.DataItem, "[id]"))%>' Mode="small"></cms:stars>
</span>
<a href="#" class="greenLink png" title="Leer más">Leer más</a>
</li>
</ItemTemplate>
</asp:Repeater>
enableViewsstate 设置为 true 或 false 并不重要。
在中继器的 ItemBound 事件上,我什至设置了代码:
protected void RptrTopRated_OnDataItemBound(object sender, RepeaterItemEventArgs e)
{
((stars)e.Item.FindControl("ctrlRating")).SetStarsRating();
}
它确定哪个输入应该选择属性(“checked”)来检查:
public void SetStarsRating() {
int rate = GetRate();
this.starHolder.Attributes["title"] = McID.ToString();
if(star1.Attributes["checked"] != null) star1.Attributes.Remove("checked");
if(star2.Attributes["checked"] != null) star2.Attributes.Remove("checked");
if(star3.Attributes["checked"] != null) star3.Attributes.Remove("checked");
if(star4.Attributes["checked"] != null) star4.Attributes.Remove("checked");
if(star5.Attributes["checked"] != null) star5.Attributes.Remove("checked");
switch (rate)
{
case 1:
star1.Attributes["checked"] = "checked";
break;
case 2:
star2.Attributes["checked"] = "checked";
break;
case 3:
star3.Attributes["checked"] = "checked";
break;
case 4:
star4.Attributes["checked"] = "checked";
break;
case 5:
star5.Attributes["checked"] = "checked";
break;
}
}
在调试过程中,我可以看到代码以正确的方式运行,并在良好的一颗星上设置了检查属性,但是有渲染的 HTML 中没有结果。
I've got my own user control inside repeater. It displays stars according to list element rating. On the first bind everything is ok, number of stars is ok for each item. Then I make a postback in which I filter this list. Then result data bind to the repeater. Lets say at first I had 5 elemetns after filtering only 3 but and only 3 are visible but the stars are in order as they were before binding.
What I would like to have is during postback each one control which is inside repeater should be recreated and appropriate number of stars whould be displayed (not the old one).
What am I doing wrong?
The repeater is inside an updatePanel.
here is my control "stars" code:
<span runat="server" id="starHolder" class="starsHolder" title="">
<input runat="server" id="star1" name="star3" type="radio" class="star1" value="1" />
<input runat="server" id="star2" name="star3" type="radio" class="star1" value="2" />
<input runat="server" id="star3" name="star3" type="radio" class="star1" value="3" />
<input runat="server" id="star4" name="star3" type="radio" class="star1" value="4"/>
<input runat="server" id="star5" name="star3" type="radio" class="star1" value="5"/>
</span>
and here is my repeater code:
<asp:Repeater EnableViewState="false" OnItemDataBound="RptrTopRated_OnDataItemBound" ID="rptrTopRated" runat="server">
<ItemTemplate>
<li class="topRated" id='<%#DataBinder.Eval(Container.DataItem, "[id]")%>'>
<p>
<%# DataBinder.Eval(Container.DataItem, "[title]") %>
</p>
<span class="ratingStar">
<cms:stars EnableViewState="false" runat="server" id="ctrlRating" McID='<%#System.Convert.ToInt32(DataBinder.Eval(Container.DataItem, "[id]"))%>' Mode="small"></cms:stars>
</span>
<a href="#" class="greenLink png" title="Leer más">Leer más</a>
</li>
</ItemTemplate>
</asp:Repeater>
It doesn't matter if enableViewsstate is set to true or false.
On ItemBound event of the repeater I even set the code:
protected void RptrTopRated_OnDataItemBound(object sender, RepeaterItemEventArgs e)
{
((stars)e.Item.FindControl("ctrlRating")).SetStarsRating();
}
which determines which input should have attrbute("checked") selected to checked:
public void SetStarsRating() {
int rate = GetRate();
this.starHolder.Attributes["title"] = McID.ToString();
if(star1.Attributes["checked"] != null) star1.Attributes.Remove("checked");
if(star2.Attributes["checked"] != null) star2.Attributes.Remove("checked");
if(star3.Attributes["checked"] != null) star3.Attributes.Remove("checked");
if(star4.Attributes["checked"] != null) star4.Attributes.Remove("checked");
if(star5.Attributes["checked"] != null) star5.Attributes.Remove("checked");
switch (rate)
{
case 1:
star1.Attributes["checked"] = "checked";
break;
case 2:
star2.Attributes["checked"] = "checked";
break;
case 3:
star3.Attributes["checked"] = "checked";
break;
case 4:
star4.Attributes["checked"] = "checked";
break;
case 5:
star5.Attributes["checked"] = "checked";
break;
}
}
During debugging I can see that code behaves in right way and sets checked attribute on good one star but there is no result in rendered HTML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会考虑使用 ASP.Net Ajax Rating 控件,这将为您节省大量时间。
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Rating /评级.aspx
You might consider using ASP.Net Ajax Rating control, which will save you a lot of time.
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Rating/Rating.aspx
我在星形控件的 Render 中添加了方法,它开始正常工作:
I added method in Render in my star control and it started to work as it should: