473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamically loaded User Controls - problem with button_click event

[this is a simplified / clearer explanation of an earlier post - hopefully
someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page.
I have the controls loading dynamically, and that part works fine. this is
the code used in a webform to dynamically load one of several controls:
private void btnCategory_Click(object sender, System.EventArgs e)

{
Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the relevant
control perfectly into the placeholder on the hosting page.

The dynamically loaded control has its own form and button within it. when a
user fills out the form and clicks the 'send' button, the button_click event
within the control is supposed to do some work and add the form data to a
DB.

My problem is that when the button from within a dynamically loaded control
is clicked, nothing happens for that event. All that happens is the page
hosting the control is reloaded, and the control disappears.

I must add that when the same control is added to a test page, everything
works fine as planned - the click event for the button does as it is
supposed to - so there is nothing wrong there. The problem is arising
because this is a DYNAMICALLY loaded control sitting in a placeholder.

I hope someone can see my exact problem and knows the solution. I'm sure it
is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.

Tim..
Nov 17 '05 #1
7 3237
HD
Just did add something on earlier post and saw the more recent one after
that.

Well does the page you are dynamically have its own form and on select you
load a user control with its own form.

I think there might be some issues with more than one form (I remember
reading it a few days back when I started with web controls)

Regards,

HD

"Tim T" <br*********@hotmail.com> wrote in message
news:bn***********@news.wplus.net...
[this is a simplified / clearer explanation of an earlier post - hopefully
someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page.
I have the controls loading dynamically, and that part works fine. this is
the code used in a webform to dynamically load one of several controls:
private void btnCategory_Click(object sender, System.EventArgs e)

{
Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the relevant control perfectly into the placeholder on the hosting page.

The dynamically loaded control has its own form and button within it. when a user fills out the form and clicks the 'send' button, the button_click event within the control is supposed to do some work and add the form data to a
DB.

My problem is that when the button from within a dynamically loaded control is clicked, nothing happens for that event. All that happens is the page
hosting the control is reloaded, and the control disappears.

I must add that when the same control is added to a test page, everything
works fine as planned - the click event for the button does as it is
supposed to - so there is nothing wrong there. The problem is arising
because this is a DYNAMICALLY loaded control sitting in a placeholder.

I hope someone can see my exact problem and knows the solution. I'm sure it is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.

Tim..

Nov 17 '05 #2
When the page reloads after the postback do you reset the
value of the placeholder?

I have found that you need to make sure that the
information is only reset when you want it to be and not
on every page refresh.
-----Original Message-----
Just did add something on earlier post and saw the more recent one afterthat.

Well does the page you are dynamically have its own form and on select youload a user control with its own form.

I think there might be some issues with more than one form (I rememberreading it a few days back when I started with web controls)
Regards,

HD

"Tim T" <br*********@hotmail.com> wrote in message
news:bn***********@news.wplus.net...
[this is a simplified / clearer explanation of an earlier post - hopefully someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e)
{
Control myControl = LoadControl (DropDownList1.SelectedItem.Text + ".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the
relevant
control perfectly into the placeholder on the hosting
page.
The dynamically loaded control has its own form and button within it. whena
user fills out the form and clicks the 'send' button,
the button_clickevent
within the control is supposed to do some work and add
the form data to a DB.

My problem is that when the button from within a dynamically loadedcontrol
is clicked, nothing happens for that event. All that
happens is the page hosting the control is reloaded, and the control disappears.
I must add that when the same control is added to a test page, everything works fine as planned - the click event for the button does as it is supposed to - so there is nothing wrong there. The problem is arising because this is a DYNAMICALLY loaded control sitting in a placeholder.
I hope someone can see my exact problem and knows the

solution. I'm sureit
is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.

Tim..

.

Nov 17 '05 #3
"Tim T" <br*********@hotmail.com> wrote in message
news:bn***********@news.wplus.net...
[this is a simplified / clearer explanation of an earlier post - hopefully
someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page.
I have the controls loading dynamically, and that part works fine. this is
the code used in a webform to dynamically load one of several controls:
private void btnCategory_Click(object sender, System.EventArgs e)

{
Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the relevant control perfectly into the placeholder on the hosting page.

The dynamically loaded control has its own form and button within it. when a user fills out the form and clicks the 'send' button, the button_click event within the control is supposed to do some work and add the form data to a
DB.

My problem is that when the button from within a dynamically loaded control is clicked, nothing happens for that event. All that happens is the page
hosting the control is reloaded, and the control disappears.

I must add that when the same control is added to a test page, everything
works fine as planned - the click event for the button does as it is
supposed to - so there is nothing wrong there. The problem is arising
because this is a DYNAMICALLY loaded control sitting in a placeholder.

I hope someone can see my exact problem and knows the solution. I'm sure it is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.


When you say the user control has its own form, do you mean it has another
<form> element within it? If so, do you really need that?
--
John
Nov 17 '05 #4
I am guessing that the problem is in the hosting form. You should post your
Page_Load code so we can take a look at that. You most likely have a problem
that can be solved by moving some code into a ...

if(!(IsPostBack))
{
....
}

section of code.

I hope that helps a bit.

"Tim T" <br*********@hotmail.com> wrote in message
news:bn***********@news.wplus.net...
[this is a simplified / clearer explanation of an earlier post - hopefully
someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page.
I have the controls loading dynamically, and that part works fine. this is
the code used in a webform to dynamically load one of several controls:
private void btnCategory_Click(object sender, System.EventArgs e)

{
Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the relevant control perfectly into the placeholder on the hosting page.

The dynamically loaded control has its own form and button within it. when a user fills out the form and clicks the 'send' button, the button_click event within the control is supposed to do some work and add the form data to a
DB.

My problem is that when the button from within a dynamically loaded control is clicked, nothing happens for that event. All that happens is the page
hosting the control is reloaded, and the control disappears.

I must add that when the same control is added to a test page, everything
works fine as planned - the click event for the button does as it is
supposed to - so there is nothing wrong there. The problem is arising
because this is a DYNAMICALLY loaded control sitting in a placeholder.

I hope someone can see my exact problem and knows the solution. I'm sure it is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.

Tim..

Nov 17 '05 #5
Thanks Adam,
doing as you suggested enables a control placed in a placeholder to work
from within the page propertly as i need,
But now I have a new problem. I still can't do what I am trying to achieve
because the control I want to load into the placeholder is determined by
what a user selects from a dropdown in the [hosting] page.

I put the code loading the control into a Page_Init sub as suggested, it
works when i statically Load a control by hard coding the control name in
the LoadControl("test.ascx"), but i need it to load the control dynamically

here is the sub:

private void Page_Init(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
string controlName = DropDownList1.SelectedItem.Text; //the text for
dropdown items is the control to load
Control myControl = LoadControl(controlName+".ascx");
PlaceHolder1.Controls.Add(myControl);
}
}

the page is posted back when a user selects form the dropdown and clicks a
button, here is the sub that handles the button click:

private void btnCategory_Click(object sender, System.EventArgs e)
{
selectecdCategory.Text = DropDownList1.SelectedItem.Text.Replace("
","").Replace("/","").Replace("&","");

}

when the user selects a categroy and clicks the button, the page posts back,
I get this error

I get the following error:

"Object reference not set to an instance of an object. "

for this line:
string controlName = DropDownList1.SelectedItem.Text;

So the DropDownList1 object is not initialised before the Page_Init sub
fires.

Can you see what i am trying to achieve? any further suggestions
appreciated.
Thanks to everyone for their input so far.

Tim..

"Adam Carden" <ad*********@futurejester.co.uk> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...


I had the same problem however the answer is simple, control binding /
view state preperation starts before page_load and any event.

If you place your control loading logic in page_init sub all will work
properly.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #6
Thanks HD,
There is only one <form runat="server"> tag on the page hosting the control,
no form tag in the user control, i meant form fields and a button

I think i'm getting closer to solving the problem, have done what Adam
Carden suggested and that works for loading controls into place holders, but
i am still having trouble getting them to load dynamically. i may have to
try a different approach soon!

Tim

"HD" <he***********@CAPShotmail.com> wrote in message
news:u6**************@TK2MSFTNGP09.phx.gbl...
Just did add something on earlier post and saw the more recent one after
that.

Well does the page you are dynamically have its own form and on select you
load a user control with its own form.

I think there might be some issues with more than one form (I remember
reading it a few days back when I started with web controls)

Regards,

HD

"Tim T" <br*********@hotmail.com> wrote in message
news:bn***********@news.wplus.net...
[this is a simplified / clearer explanation of an earlier post - hopefully someone can help!]

Hi,
I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls:
private void btnCategory_Click(object sender, System.EventArgs e)

{
Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
".ascx");
PlaceHolder1.Controls.Add(myControl);
}

That part works fine, selecting an item from the dropdown loads the relevant
control perfectly into the placeholder on the hosting page.

The dynamically loaded control has its own form and button within it. when a
user fills out the form and clicks the 'send' button, the button_click

event
within the control is supposed to do some work and add the form data to

a DB.

My problem is that when the button from within a dynamically loaded

control
is clicked, nothing happens for that event. All that happens is the page hosting the control is reloaded, and the control disappears.

I must add that when the same control is added to a test page, everything works fine as planned - the click event for the button does as it is
supposed to - so there is nothing wrong there. The problem is arising
because this is a DYNAMICALLY loaded control sitting in a placeholder.

I hope someone can see my exact problem and knows the solution. I'm sure

it
is something simple, but am new to .NET and am stuck.
Any help greatly appreciated.

Tim..


Nov 17 '05 #7

I do have a solution to the other part of your problem, however it may
not be what you want to here.

As control events fire after page_init you could not use them to
dynamically load control in page_init.
You will still be doing a postback so it should be ok.

->In page_init

If Request.Form("dropdownaction") = True Then
'Load the control specified in the named drop down
(your placeholder control)

Panel1.Controls.Add(UserControl.LoadControl(Reques t.Form("<thenameofyour
dropdowncontrol>"))
End If

->In your page/control with the control selector
(within the <form> </form>)

<input type=hidden name=dropdownaction value=false>

-> As an attibute added to your dropdownlist
onchange="loadcontrol();"

-> As a script block on the page
<script langauge="javascript">
function loadcontrol() {
yourform.dropdownaction = "true";
yourform.submit();
}
</script>

I know the example is not complete, nor actually working as I have
little time. I do hope it helps.

The gist of the matter is as postbacks post the form all standard
selects (which is what drop downs render as) still pass the selected
value when the form is posted.

You can access both request.querystring and request.form in page_init so
you can get the control value from there and load the correct control.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #8

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

Similar topics

8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
1
by: Greg | last post by:
In my ASP.NET application I have a button that when pressed, data needs to be saved based on what the user typed in. I have other controls on the same page that should be updated as a result of the...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
3
by: supvine | last post by:
Hello, I am developing a page that has several custom web user controls that need to be loaded dynamically. Some of the controls are loaded based on the action (button-click) of a different...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
7
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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...
0
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
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,...

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.