在Repeater_ItemDataBound事件中设置用户控制属性? [英] Set User Control Property in Repeater_ItemDataBound event?
问题描述
我有一个用户控件,其中包含的属性很少。但我不知道如何在Repeater_ItemDataBound事件中设置它们。
< asp:Repeater ID = Repeater1 runat = server
onitemdatabound = Repeater1_ItemDataBound >
< ItemTemplate>
< UC:ParentChildTree ID = ParentChildTree1 runat = server />
< / ItemTemplate >
< / asp:Repeater >
代码落后:
UserControl ParentChildTree1 =(UserControl)item.FindControl( ParentChildTree1);
// 以这种方式设置属性
如果不在Repeater中,我可以这样设置属性:
< UC:ParentChildTree ID = ParentChildTree1 Property1 = value1 runat = server / >
或ParentChildTree1.Property1 =value1;
帮助。
试试这个..
protected void Repeater1_ItemDataBound( object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Alter natingItem)
{
var ParentChildTree1 =(UserControl的类型)e.item.FindControl( ParentChildTree1);
}
}
learner'sbug,我已经尝试过了:
< pre lang =c#> if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var ParentChildTree1 =(ParentChildTree)e.Item.FindControl( ParentChildTree1\" 跨度>);
}
错误1类型或 命名空间名称ParentChildTree可以找到 (你错过了< span class =code-keyword>使用指令或汇编参考?)
我< span class =code-comment>' 知道用户控件类型,案例似乎不起作用。
和这不会' t work:
< ; pre lang = c#>
var ParentChildTree1 =(MyNamespace.ParentChildTree)e.Item.FindControl( ParentChildTree1) ;
错误2MyNamespace类型中不存在类型名称ParentChildTree
< blockquote>我偶然发现了这个解决方案:
protected void Page_Load( object sender,EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write(LoadControl( 〜/控制/ ParentChildTreeView.ascx跨度>)的GetType()的ToString());
}
}
输出为:ASP .controls_parentchildtreeview_ascx
所以我这样投:
var ParentChildTree1 =(ASP.controls_parentchildtreeview_ascx)e.Item.FindControl( ParentChildTree1) ;
这样可行!
我可以在代码后面设置我的UC属性。
I have an user contorls that have few properties that must set. But I don't know how to set them in Repeater_ItemDataBound event.
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound" >
<ItemTemplate>
<UC:ParentChildTree ID="ParentChildTree1" runat="server" />
</ItemTemplate>
</asp:Repeater>
code behind:
UserControl ParentChildTree1 = (UserControl)item.FindControl("ParentChildTree1");
// cant' set property this way
If not in Repeater, I can set property this way :
<UC:ParentChildTree ID="ParentChildTree1" Property1="value1" runat="server" />
or ParentChildTree1.Property1="value1";
Help.
Try this..
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { var ParentChildTree1 = (Type of UserControl)e.item.FindControl("ParentChildTree1"); } }
Hi, "learner'sbug", I already try this:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { var ParentChildTree1 = (ParentChildTree)e.Item.FindControl("ParentChildTree1"); }
Error 1 The typeor namespace name ParentChildTree could not be found (are you missing a using directive or an assembly reference?) I don't know the User Controls type, and the case seems not work. and this doesn't work: <pre lang="c#"> var ParentChildTree1 = (MyNamespace.ParentChildTree)e.Item.FindControl("ParentChildTree1");
Error 2 The type name 'ParentChildTree' does not exist in the type 'MyNamespace'
I find this solution by accident:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Response.Write(LoadControl("~/Controls/ParentChildTreeView.ascx").GetType().ToString()); } }
The output is : ASP.controls_parentchildtreeview_ascx
So I cast like this:
var ParentChildTree1 = (ASP.controls_parentchildtreeview_ascx)e.Item.FindControl("ParentChildTree1");
And this works!
I can set my UC's property in code behind.
这篇关于在Repeater_ItemDataBound事件中设置用户控制属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!