[英]ASP.net access control in FormView ItemTemplate
我有一个带有控件的项目模板的表单视图,是否可以访问该控件OnDatabound,因此我可以将控件与数据绑定。 我在这里使用面板作为例子。
<cc1:LOEDFormView ID="FireFormView" runat="server" DataSourceID="DataSourceResults" CssClass="EditForm" DataKeyNames="id" OnDatabound="FireFromView_Databound">
<ItemTemplate>
<asp:Panel ID ="pnl" runat="server"></asp:Panel>
</ItemTemplate>
</cc1:LOEDFormView>
您必须注意项目模式以及您想要查找的控件。 就像你在物品模板中的控制一样,那就像...
if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{
Panel pnl = (Panel)FormView1.FindControl("pnl");
}
下面的代码解决了我的问题。 虽然该示例访问标签,但它适用于大多数控件。 您只需要向FormView
添加一个DataBound
事件。
protected void FormView1_DataBound(object sender, EventArgs e)
{
//check the formview mode
if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{
//Check the RowType to where the Control is placed
if (FormView1.Row.RowType == DataControlRowType.DataRow)
{
Label label1 = (Label)UserProfileFormView.Row.Cells[0].FindControl("Label1");
if (label1 != null)
{
label1.Text = "Your text";
}
}
}
}
我没有在您的标记中看到标签,但看到了Panel。 所以要访问面板,
尝试
Panel p = FireFormView.FindControl("pnl") as Panel;
if(p != null)
{
...
}
if (FireFormView.Row != null)
{
if (FireFormView.CurrentMode == FormViewMode.ReadOnly)
{
Panel pnl = (Panel)FireFormView.FindControl("pnl");
}
else
{
//FormView is not in readonly mode
}
}
else
{
//formview not databound, check select statement and parameters.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.