Domanda

Ho un DetailsView che sto postando indietro - e all'interno di questo è un UserControl. Sto avendo qualche difficoltà trova nella dati di postback.

Per fare un esempio:

<asp:DetailsView ID="dvDetailsView" runat="Server" AutoGenerateRows="false">
<Fields>
  <asp:TemplateField>
    <ItemTemplate>
      Some text here
    </ItemTemplate>
    <EditItemTemplate>
      <uc:UserControl ID="ucUserControl" runat="server" />
    </EditItemTemplate>
    <InsertItemTemplate>
      <uc:UserControl ID="ucUserControl" runat="server" />
    </InsertItemTemplate>
  </asp:TemplateField>
</Fields>
</asp:DetailsView>
:

Quando ho postback, vorrei assumere vorrei fare qualcosa di simile:

MyUserControlType ucUserControl = dvDetailsView.FindControl("ucUserControl") as MyUserControlType;

Ma questo non trova niente. In realtà, non riesco nemmeno a trovare questo bambino camminando intorno a Controllo immediato ...

Che cosa devo fare per trovare questa cosa ??

Modifica Si scopre la mia UserControl id veniva cambiato - ma perché? Io ho lo stesso ID su entrambi i modelli Inserisci e modifica, ma commentando che il fatto alcuna differenza.

È stato utile?

Soluzione 2

As it turns out, the user control name was changed - my usercontrol, labelled as "ucUserControl" had it's name changed to a generic name - 'ctl01'.

So, doing advSituation.Rows[0].Cells[0].FindControl("ctl01") found the control.

To find this ID, I just had a look at the HTML element being rendered, and checked the parent from the id, e.g. 'ctl00_MainContent_dvDetailsView_ctl01_lblLabel', where lblLabel appeared on ucUserControl.

The rows column is a 0 based index of the number of fields, and the cells index will be 1 if you have a headertemplate specified.

EDIT: OMG! Someone (it really wasn't me, I swear) had hidden the ID property on the control class!

public partial class UserControl : BaseControl
{
  public int Id;
}

This meant that when ASP.Net was generating the id, it couldn't, and just assigned a generic Id ('ctl01' in this case) to the control, rather than the actual name.

Wow.

Altri suggerimenti

Dopo DataBinding il controllo, utilizza:

dvDetailsView.Rows[0].Cells[0].FindControl("ucUserControl")

E assicurarsi che si sta facendo questo solo in modalità Modifica come il controllo esiste solo in EditItemTemplate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top