473,508 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically added user control

I am dynamically adding a user control to each row in a gridview. The reason
I am doing it dynamically is the user control is different depending on
certain data in the gridview. The gridview contains a placeholder and I add
the control to it, the user control is a formview bound to an object
datsource. This works great until I post back the page and the user control
disappears. What am I doing wrong? Regards, Chris.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
ObjectDataSource)

o.SelectParameters(0).DefaultValue =
GridView1.DataKeys(e.Row.DataItemIndex).Value

Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)

Dim ctl As Control = LoadControl("updateorder.ascx")

Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
ObjectDataSource)

odssub.SelectParameters("customerid").DefaultValue =
CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value )

pl.Controls.Add(ctl)

End If

End Sub
Apr 1 '07 #1
9 7907
You have to re-create dynamically added controls on every postback,
preferably in Page_Init event. It could be easier to cater for all possible
controls in the ItemTemplate and show/hide them as required with css rule
display:none.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Chris" <no****@nospam.comwrote in message
news:Oe**************@TK2MSFTNGP03.phx.gbl...
>I am dynamically adding a user control to each row in a gridview. The
reason I am doing it dynamically is the user control is different depending
on certain data in the gridview. The gridview contains a placeholder and I
add the control to it, the user control is a formview bound to an object
datsource. This works great until I post back the page and the user control
disappears. What am I doing wrong? Regards, Chris.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
ObjectDataSource)

o.SelectParameters(0).DefaultValue =
GridView1.DataKeys(e.Row.DataItemIndex).Value

Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)

Dim ctl As Control = LoadControl("updateorder.ascx")

Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
ObjectDataSource)

odssub.SelectParameters("customerid").DefaultValue =
CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value )

pl.Controls.Add(ctl)

End If

End Sub


Apr 1 '07 #2
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. Regards, Chris.

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:ev**************@TK2MSFTNGP03.phx.gbl...
You have to re-create dynamically added controls on every postback,
preferably in Page_Init event. It could be easier to cater for all
possible controls in the ItemTemplate and show/hide them as required with
css rule display:none.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Chris" <no****@nospam.comwrote in message
news:Oe**************@TK2MSFTNGP03.phx.gbl...
>>I am dynamically adding a user control to each row in a gridview. The
reason I am doing it dynamically is the user control is different
depending on certain data in the gridview. The gridview contains a
placeholder and I add the control to it, the user control is a formview
bound to an object datsource. This works great until I post back the page
and the user control disappears. What am I doing wrong? Regards, Chris.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
ObjectDataSource)

o.SelectParameters(0).DefaultValue =
GridView1.DataKeys(e.Row.DataItemIndex).Value

Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"),
PlaceHolder)

Dim ctl As Control = LoadControl("updateorder.ascx")

Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
ObjectDataSource)

odssub.SelectParameters("customerid").DefaultValu e =
CInt(GridView1.DataKeys(e.Row.DataItemIndex).Valu e)

pl.Controls.Add(ctl)

End If

End Sub



Apr 1 '07 #3
"Chris" <no****@nospam.comwrote in message
news:OJ**************@TK2MSFTNGP03.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...
Apr 1 '07 #4
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.FindControl("plchildgw"), 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.EventArgs)
Handles Me.Init

mastercontrol = LoadControl("updateorder.ascx")

End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
ObjectDataSource)

o.SelectParameters(0).DefaultValue =
GridView1.DataKeys(e.Row.DataItemIndex).Value

Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)

Dim odssub As ObjectDataSource =
CType(mastercontrol.FindControl("odscustomer"), ObjectDataSource)

odssub.SelectParameters("customerid").DefaultValue =
CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value )

pl.Controls.Add(mastercontrol)

End If

End Sub

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Chris" <no****@nospam.comwrote in message
news:OJ**************@TK2MSFTNGP03.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...

Apr 1 '07 #5
"Chris" <no****@nospam.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
Here is the code where I try to bind later

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init

mastercontrol = LoadControl("updateorder.ascx")

End Sub
That creates *one* dynamic control.
but the control only binds to the last element in the gridview, which may
be some thing stupid I have done.
You need as many dynamic controls as there are rows in the GridView...
Apr 1 '07 #6
I've got it working with this. It makes sense to me but if you can see
potential problems.....

Dim row As GridViewRow

Dim i As Integer = 0

For Each row In GridView1.Rows

Dim txt As New TextBox()

Dim control As New Control()

mastercontrol = LoadControl("updateorder.ascx")

Dim odssub As ObjectDataSource =
CType(mastercontrol.FindControl("odscustomer"), ObjectDataSource)

odssub.SelectParameters("customerid").DefaultValue =
GridView1.DataKeys(row.RowIndex).Value

Dim pl As PlaceHolder = CType(row.FindControl("plchildgw"), PlaceHolder)

pl.Controls.Add(mastercontrol)

Next


"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:e8**************@TK2MSFTNGP02.phx.gbl...
"Chris" <no****@nospam.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
>Here is the code where I try to bind later

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init

mastercontrol = LoadControl("updateorder.ascx")

End Sub

That creates *one* dynamic control.
>but the control only binds to the last element in the gridview, which may
be some thing stupid I have done.

You need as many dynamic controls as there are rows in the GridView...

Apr 1 '07 #7
"Chris" <no****@nospam.comwrote in message
news:OL**************@TK2MSFTNGP05.phx.gbl...
Dim txt As New TextBox()
Dim control As New Control()
What are those two variables for...? You don't appear to be using them
anywhere...
Apr 1 '07 #8
They've been left in by accident :)

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uG**************@TK2MSFTNGP02.phx.gbl...
"Chris" <no****@nospam.comwrote in message
news:OL**************@TK2MSFTNGP05.phx.gbl...
>Dim txt As New TextBox()
Dim control As New Control()

What are those two variables for...? You don't appear to be using them
anywhere...

Apr 1 '07 #9
"Chris" <no****@nospam.comwrote in message
news:el*************@TK2MSFTNGP05.phx.gbl...
They've been left in by accident :)
Ah... :-)
Apr 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1710
by: Jon B | last post by:
Hi There! How to handle the events of a dynamically added user control? e.g. I have following code... Dim myUserControl as Object = LoadControl("myFirstControl.ascx") myFirstControl fires...
7
3176
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
1
1492
by: andyrich_1 | last post by:
HI, I'm adding a usercontrol to a webform in the following way Dim ctlRate As New ctlRatesTable ctlRate.SomeMethod() Me.Page.Controls.Add(ctlRate) Where sub routine "SomeMethod" attempts...
0
1125
by: jburnage | last post by:
I need help in getting the values from a dynamically added user control when a user submits a form. The controls are added to a placeholder and this all works fine - but its looping through the...
4
1402
by: Jenni.Haughton | last post by:
I have a solution with 2 projects in it. I need to programatically add a user control from one project into the other one (as you cannot declare this normally on the form because it is in a...
1
2139
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I...
7
6633
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.