Load usercontrol in notPage ispostback

S

sebastien

Hi,
In page_load :

If Not Page.IsPostBack Then
Dim myUC As UserControl = LoadControl("ficheProspects.ascx")
myUC.ID = "FicheProspects1"
End If

In another Sub i need to find my control :

Dim myUC As UserControl = CType((FindControl("FicheProspects1")), UserControl)
Dim ucType As Type = myUC.GetType()
Dim monIdUC As System.Reflection.PropertyInfo = ucType.GetProperty("monId")
' Set the property
monIdUC.SetValue(myUC, dgAdminProspects.DataKeys(e.Item.ItemIndex), Nothing)

But i have don't succes to find it :(
 
B

Brock Allen

Once you've created it, you need to add it to the page somewhere. So use
a PlaceHolder or some other "container" control and call MyPlaceHolder.Controls.Add(myUC)
 
S

Sébastien ....

Look my problem :

PAGE ASPX :

#Region " - Page_Load - "
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
monStyle.Text = Session("style")
bindData()
Session("selectionligne") = -1
End If
End Sub
#End Region

#Region " - bindData - "
Public Sub bindData()
daAdminProspects.Fill(Bts1)
dgAdminProspects.DataSource = myDataview
dgAdminProspects.DataBind()
End Sub
#End Region

#Region " - ItemDataBound - "
Private Sub dgClient_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgAdminProspects.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim imgDelete As ImageButton =
CType(e.Item.FindControl("imgDelete"), ImageButton)
CreateConfirmBox(imgDelete, "Attention, merci de confirmer
la suppression.")
End If
End Sub
#End Region

#Region " - ItemCommand - "
Private Sub dgAdminProspects_ItemCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgAdminProspects.ItemCommand
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.CommandName = "Select" Then
dgAdminProspects.EditItemIndex = -1
bindData()
End If
End If
End Sub

#End Region

#Region " - SelectedIndexChanged - "
Private Sub dgAdminProspects_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
dgAdminProspects.SelectedIndexChanged
If dgAdminProspects.SelectedIndex = Session("selectionLigne")
Then
dgAdminProspects.SelectedIndex = -1
Session("selectionLigne") = -1
bindData()
Else
'Pas le meme => initialise la session("selectionLigne")
Session("selectionLigne") = dgAdminProspects.SelectedIndex
End If
End Sub
#End Region

#Region " - PreRender - "
Private Sub dgAdminProspects_PreRender(ByVal sender As Object, ByVal
e As System.EventArgs) Handles dgAdminProspects.PreRender
If dgAdminProspects.SelectedIndex > -1 Then
Dim oDGItem As New DataGridItem(0, 0, ListItemType.Item)

Dim oCell As New TableCell
oCell.HorizontalAlign = HorizontalAlign.Left
oCell.ColumnSpan = dgAdminProspects.Columns.Count
oDGItem.Cells.Add(oCell)

Dim myUC As ficheProspects =
CType(Page.LoadControl("ficheProspects.ascx"), ficheProspects)
myUC.monId =
CType(dgAdminProspects.DataKeys(dgAdminProspects.SelectedIndex),
Integer)

'Vérifie si la couleur est alternative
If dgAdminProspects.SelectedIndex Mod 2 <> 0 Then
myUC.backcouleur =
CType(dgAdminProspects.AlternatingItemStyle.BackColor,
System.Drawing.Color)
oCell.BackColor =
dgAdminProspects.AlternatingItemStyle.BackColor
End If

oCell.Controls.Add(myUC)


dgAdminProspects.Controls(0).Controls.AddAt(dgAdminProspects.SelectedInd
ex + 3, oDGItem)
End If


End Sub
#End Region
---------------------------------------------
USERCONTROL : i have a panel, a table, and a button

'Variable public pour communiquer avec la page .aspx
Private _backcouleur As System.Drawing.Color
Public Property backcouleur() As System.Drawing.Color
Get
Return _backcouleur
End Get
Set(ByVal Value As System.Drawing.Color)
_backcouleur = Value
End Set
End Property

Private _monId As Integer
Public Property monId() As Integer
Get
Return _monId
End Get
Set(ByVal Value As Integer)
_monId = Value
End Set
End Property



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Changer le background des panel
panelProspects.BackColor = backcouleur
panelProspectsUpdate.BackColor = backcouleur

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Response.Write("TEST")

End Sub
 
B

Brock Allen

I can't quite tell what's wrong about it. This code is different than you
roriginal post, though. This code seems to dynamically load the user control
in PreRender not Page_Load. The only thing I'd say about that is make sure
any dynamically loaded controls are recreated upon postback. You should probabaly
do this in Page_Load (or earlier) so that all of the control IDs are in line
with how the original page rendered so that when there is a postback things
will work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top