FormView ItemTemplate中的ASP.net访问控制
我有一个带有控件的项目模板的表单视图,是否可以访问该控件OnDatabound,因此我可以将控件与数据绑定。 我在这里使用面板作为例子。
您必须注意项目模式以及您想要查找的控件。 就像你在项目模板中的控件一样,那就像…
if (FormView1.CurrentMode == FormViewMode.ReadOnly) { Panel pnl = (Panel)FormView1.FindControl("pnl"); }
我没有在您的标记中看到标签但是看到了Panel。 所以要访问面板,
尝试
Panel p = FireFormView.FindControl("pnl") as Panel; if(p != null) { ... }
以下代码解决了我的问题。 虽然该示例访问标签,但它适用于大多数控件。 您只需要向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"; } } } }
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. }