ASP.net Access Control in FormView ItemTemplate

I have a form view with an element template with a control inside, is it possible to access this OnDatabound control so that I can bind the control to data. I use the panel as an example here.

<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> 
+4
source share
4 answers

You also need to take care of the element mode in which your control exists that you want to find. For example, if your control is in an Element Template, then it will look like.

 if (FormView1.CurrentMode == FormViewMode.ReadOnly) { Panel pnl = (Panel)FormView1.FindControl("pnl"); } 
+9
source

I do not see a shortcut in your markup, but I see a panel. Therefore, to access the panel,

Try

 Panel p = FireFormView.FindControl("pnl") as Panel; if(p != null) { ... } 
+1
source
  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. } 
0
source

This code below solved my problem. Although in this example, label access is applied to most controls. You just need to add the DataBound event to your FormView .

 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"; } } } } 
0
source

All Articles