ASP.NET Dynamic User Controls

I am sure that this question has been asked a million times, but I could not find an answer that solves my problem.

I programmatically add some user controls to the PlaceHolder that I have on a simple aspx page. All Postback user controls work correctly, except for those that contain Gridview.

For some reason, any postback that is triggered from this control does not trigger the specified event on the first click, however, all future clicks will work fine. I have no idea why this is so, but many of the solutions I found suggest adding an identifier to the ascx user element, but this does not work in my case.

I looked at the source file for the page that is generated before and after the first click, javascript used to call postback changes, i.e.

Before the first click: onclick = "javascript: __ doPostBack ('tmpControlID $ sgvPrimaryEmploymentHistory', 'Select $ 0')"

After the first click: onclick = "javascript: __ doPostBack ('OFFHome1 $ tmpControlID $ sgvPrimaryEmploymentHistory', 'Select $ 0')"

OFFHome1 is the parent user control that exists on an aspx page. All other controls are added to the placeholder in this control, i.e.

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="OFFHome.ascx.vb" Inherits="UmbracoUserControls.OFFHome" %>
<asp:PlaceHolder ID="phOFFSection" runat="server"></asp:PlaceHolder>

Nothing complicated. Then, in the code behind the controls, they are loaded into the placeholder using the following:

Private Sub LoadNextOFFStep()
    Dim ControlName As String = "TestControl.ascx"
    phOFFSection.Controls.Clear()
    If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
        Dim NewControl As Object = LoadControl(ControlName)

        With NewControl
            .ID = USERCONTROLNAME
            Dim StepCompleted As Boolean = .FillControl()
            If StepCompleted Then
                Exit Sub
            End If
            Dim AllowSkip As Boolean = .AllowSkip()
            btnSkip.Visible = AllowSkip
        End With
        phOFFSection.Controls.Add(NewControl)
    End If
End Sub

, . USERCONTROLNAME - "tmpControlID".

, , , GridView, , asp, , , .

, , , , . Page_Load, .

?

+3
2

, , , , .

, , . FillControl , User Control PlaceHolder. , User Control PlaceHolder, .

:

Private Sub LoadNextOFFStep()
    Dim ControlName As String = "TestControl.ascx"
    phOFFSection.Controls.Clear()
    If ControlName IsNot Nothing AndAlso ControlName <> String.Empty Then
        Dim NewControl As Object = LoadControl(ControlName)

        With NewControl
            .ID = USERCONTROLNAME
            Dim AllowSkip As Boolean = .AllowSkip()
            btnSkip.Visible = AllowSkip
        End With
        phOFFSection.Controls.Add(NewControl)
        Dim StepCompleted As Boolean = CType(phOFFSection.Controls(0), Object).FillControl()
        If StepCompleted Then
            LoadNextOFFStep()
            Exit Sub
        End If
    End If
End Sub

.

+1

... : , (, postback script) . , .

, ... ! , , - .

NamingContainer NewControl:

    With NewControl
        .NamingContainer = OffHomeOne; // whatever
        .ID = USERCONTROLNAME
0

Source: https://habr.com/ru/post/1729013/


All Articles