I need help, I've looking at this for too long. I need to add the controls
on every postback but when I load the control in the init and add it on
databinding it still disappears on postback. I've been having more success
with adding the controls on the page load.
For Each row In GridView1.Rows
Dim pl As PlaceHolder = CType(row.FindC ontrol("plchild gw"), PlaceHolder)
pl.Controls.Add (mastercontrol)
Next
but the control only binds to the last element in the gridview, which may be
some thing stupid I have done. Thanks for the help ;)
Here is the code where I try to bind later
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Init
mastercontrol = LoadControl("up dateorder.ascx" )
End Sub
Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles
GridView1.RowDa taBound
If e.Row.RowType = DataControlRowT ype.DataRow Then
Dim o As ObjectDataSourc e = CType(e.Row.Fin dControl("objds getorders"),
ObjectDataSourc e)
o.SelectParamet ers(0).DefaultV alue =
GridView1.DataK eys(e.Row.DataI temIndex).Value
Dim pl As PlaceHolder = CType(e.Row.Fin dControl("plchi ldgw"), PlaceHolder)
Dim odssub As ObjectDataSourc e =
CType(mastercon trol.FindContro l("odscustomer" ), ObjectDataSourc e)
odssub.SelectPa rameters("custo merid").Default Value =
CInt(GridView1. DataKeys(e.Row. DataItemIndex). Value)
pl.Controls.Add (mastercontrol)
End If
End Sub
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
"Chris" <no****@nospam. comwrote in message
news:OJ******** ******@TK2MSFTN GP03.phx.gbl...
>>I kind of like the user control idea as it is a little more modular. I am
going to end end with about 20 variations of user control, doing it in the
item template seem more complex. How would I go about adding the user
controls to a gridview from the page init event.
Eliyahu is right, though I think you have slightly misunderstood his
advice...
Dynamically created controls need to be dynamically created every time the
page loads - they don not persist across a postback.
However, you don't add the dynamically created controls to the GridView in
Page_Init - you simply create them there and hide them for use later.
Although dynamic controls *can* be created anywhere in code-behind, they
have a tendency not to work properly if they are created any later in the
page cycle than Page_Init - specifically, their events don't get wired up
successfully.
Thus, when you come to bind your GridView, you can use its OnDataBinding
event simply to add the dynamically created controls to the GridView as
required...