473,732 Members | 2,171 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_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 ctl As Control = LoadControl("up dateorder.ascx" )

Dim odssub As ObjectDataSourc e = CType(ctl.FindC ontrol("odscust omer"),
ObjectDataSourc e)

odssub.SelectPa rameters("custo merid").Default Value =
CInt(GridView1. DataKeys(e.Row. DataItemIndex). Value)

pl.Controls.Add (ctl)

End If

End Sub
Apr 1 '07 #1
9 7926
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******** ******@TK2MSFTN GP03.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_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 ctl As Control = LoadControl("up dateorder.ascx" )

Dim odssub As ObjectDataSourc e = CType(ctl.FindC ontrol("odscust omer"),
ObjectDataSourc e)

odssub.SelectPa rameters("custo merid").Default Value =
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.orgwro te in
message news:ev******** ******@TK2MSFTN GP03.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******** ******@TK2MSFTN GP03.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_RowDa taBound(ByVal sender As Object, ByVal e As
System.Web.UI. WebControls.Gri dViewRowEventAr gs) Handles
GridView1.RowD ataBound

If e.Row.RowType = DataControlRowT ype.DataRow Then

Dim o As ObjectDataSourc e = CType(e.Row.Fin dControl("objds getorders"),
ObjectDataSour ce)

o.SelectParame ters(0).Default Value =
GridView1.Data Keys(e.Row.Data ItemIndex).Valu e

Dim pl As PlaceHolder = CType(e.Row.Fin dControl("plchi ldgw"),
PlaceHolder)

Dim ctl As Control = LoadControl("up dateorder.ascx" )

Dim odssub As ObjectDataSourc e = CType(ctl.FindC ontrol("odscust omer"),
ObjectDataSour ce)

odssub.SelectP arameters("cust omerid").Defaul tValue =
CInt(GridView1 .DataKeys(e.Row .DataItemIndex) .Value)

pl.Controls.Ad d(ctl)

End If

End Sub



Apr 1 '07 #3
"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...
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.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...

Apr 1 '07 #5
"Chris" <no****@nospam. comwrote in message
news:Ot******** ******@TK2MSFTN GP02.phx.gbl...
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
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("up dateorder.ascx" )

Dim odssub As ObjectDataSourc e =
CType(mastercon trol.FindContro l("odscustomer" ), ObjectDataSourc e)

odssub.SelectPa rameters("custo merid").Default Value =
GridView1.DataK eys(row.RowInde x).Value

Dim pl As PlaceHolder = CType(row.FindC ontrol("plchild gw"), PlaceHolder)

pl.Controls.Add (mastercontrol)

Next


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

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

mastercontro l = LoadControl("up dateorder.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******** ******@TK2MSFTN GP05.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**@markNOSPA Mrae.comwrote in message
news:uG******** ******@TK2MSFTN GP02.phx.gbl...
"Chris" <no****@nospam. comwrote in message
news:OL******** ******@TK2MSFTN GP05.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******** *****@TK2MSFTNG P05.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
1720
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 LinkClicked event and I don't know how to handle that LinkClicked event from containing page.
7
3195
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 -- UC/ ----- Classic/
1
1506
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 to set the text property of a
0
1135
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 controls that i'm having trouble with. Its the "case 1:" statement that i need help with cheers in advance for any help my code is as follows. // a Property that manages a counter stored in ViewState protected int NumberOfSchemeControls
4
1413
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 different application). When the page_load event of the user control is fired at run-time, none of the form objects on the user control exist. Is this a namespace issue? Or can you just not share a user control over 2 projects without having a copy...
1
2158
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 want to do some form level validations. For example suppose my .ascx file contains a text box with id say "trialTextBox". Now if user hits the button thrice, 3 user controls will be there on the page and as a result 3 textboxes mentioned...
7
6664
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 tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2180
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.