473,396 Members | 1,827 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.

Load Controls from Event?

This has to be simple, but I can't seem to grasp what I'm doing wrong!
I have a user control that contains a button that when clicked raises a
"AddEvent". When this fires I want the host page to add another of the
user controls to the page.

I'm tracking the number of controls to load in a textbox on the page
(wrapped in a property) and doing a for loop in the page_load. The
problem is that the addevent is processed after the load event and
therefore the number of controls is 1 behind what it should be.

Can someone please tell me what I'm doing wrong here? Or if there is a
better approach please let me know!

protected void Page_Load(Object sender, EventArgs e)
{
for (Int16 i = 1; i <= QueryBoxCount; i++)
{
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
}

protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
}

private Int16 QueryBoxCount
{
get
{
return Convert.ToInt16(this.TextBox1.Text);
}
set
{
this.TextBox1.Text = value.ToString();
}
}

Thanks!

Jan 17 '07 #1
2 1315
protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
This works perfectly! For some reason I thought if I created controls
anywhere other than during the load event they would be destroyed on
post back. Shows what I know huh?
Please note you could use ViewState to store the counter internally instead
of textbox (but i quess in your current solution user can enter number of
QueryBoxes to display)
I didn't know I could use ViewState - I thought my only options where a
hidden field or a session variable. If you have time could you explain
how I could use ViewState here? I'm a complete beginner at this (I've
only ran through some basic tutorials).

Thanks again!
Milosz wrote:
Hi,

You cannot move creation of the controls anywhere (i.e. prerender) because
event would not be raised, therefore you could recreate controls everytime
counter changes or just add one more to the panel:

protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
or
protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;

this.Panel1.Controls.Clear();

for (Int16 i = 1; i <= QueryBoxCount; i++)
{
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
}

Please note you could use ViewState to store the counter internally instead
of textbox (but i quess in your current solution user can enter number of
QueryBoxes to display)
--
Milosz
"ju*************@yahoo.com" wrote:
This has to be simple, but I can't seem to grasp what I'm doing wrong!
I have a user control that contains a button that when clicked raises a
"AddEvent". When this fires I want the host page to add another of the
user controls to the page.

I'm tracking the number of controls to load in a textbox on the page
(wrapped in a property) and doing a for loop in the page_load. The
problem is that the addevent is processed after the load event and
therefore the number of controls is 1 behind what it should be.

Can someone please tell me what I'm doing wrong here? Or if there is a
better approach please let me know!

protected void Page_Load(Object sender, EventArgs e)
{
for (Int16 i = 1; i <= QueryBoxCount; i++)
{
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
}

protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
}

private Int16 QueryBoxCount
{
get
{
return Convert.ToInt16(this.TextBox1.Text);
}
set
{
this.TextBox1.Text = value.ToString();
}
}

Thanks!
Jan 17 '07 #2
Wow, that's pretty easy!

Thanks!
Milosz wrote:
No problem at all, Instead using TextBox to store the value between postbacks
it would be better to use viewstate, i.e.:

protected int QueryBoxCount
{
get
{
object value = ViewState["QueryBoxCount"];
return value == null ? 1 /* i'm quessing you're displaying one Query box
by default */ : (int) value;
}
set
{
ViewState["QueryBoxCount"] = value;
}
}

Done.
--
Milosz
"ju*************@yahoo.com" wrote:
protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
This works perfectly! For some reason I thought if I created controls
anywhere other than during the load event they would be destroyed on
post back. Shows what I know huh?
Please note you could use ViewState to store the counter internally instead
of textbox (but i quess in your current solution user can enter number of
QueryBoxes to display)
I didn't know I could use ViewState - I thought my only options where a
hidden field or a session variable. If you have time could you explain
how I could use ViewState here? I'm a complete beginner at this (I've
only ran through some basic tutorials).

Thanks again!
Milosz wrote:
Hi,
>
You cannot move creation of the controls anywhere (i.e. prerender) because
event would not be raised, therefore you could recreate controls everytime
counter changes or just add one more to the panel:
>
protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
or
protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
>
this.Panel1.Controls.Clear();
>
for (Int16 i = 1; i <= QueryBoxCount; i++)
{
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
}
>
Please note you could use ViewState to store the counter internally instead
of textbox (but i quess in your current solution user can enter number of
QueryBoxes to display)
--
Milosz
>
>
"ju*************@yahoo.com" wrote:
>
This has to be simple, but I can't seem to grasp what I'm doing wrong!
I have a user control that contains a button that when clicked raises a
"AddEvent". When this fires I want the host page to add another of the
user controls to the page.

I'm tracking the number of controls to load in a textbox on the page
(wrapped in a property) and doing a for loop in the page_load. The
problem is that the addevent is processed after the load event and
therefore the number of controls is 1 behind what it should be.

Can someone please tell me what I'm doing wrong here? Or if there is a
better approach please let me know!

protected void Page_Load(Object sender, EventArgs e)
{
for (Int16 i = 1; i <= QueryBoxCount; i++)
{
QueryBox qb = (QueryBox)LoadControl("QueryBox.ascx");
qb.AddEvent += new EventHandler(QueryBox1_AddEvent);
this.Panel1.Controls.Add(qb);
}
}

protected void QueryBox1_AddEvent(Object sender, EventArgs e)
{
QueryBoxCount++;
}

private Int16 QueryBoxCount
{
get
{
return Convert.ToInt16(this.TextBox1.Text);
}
set
{
this.TextBox1.Text = value.ToString();
}
}

Thanks!
Jan 17 '07 #3

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

Similar topics

0
by: ptass | last post by:
I have a main form (Form1) which opens a second form (Form2) when a button is clicked. Form2 has .FormBorderStyle set to FixedSingle. Form2 contains two buttons, one which unpins it from Form1...
6
by: Robert Werner | last post by:
I want to write one general purpose set of code that will handle the population (where required) of all controls on a form. Though I'm new to C# my instinct is to somehow hook into the Load event...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
0
by: Frank 'Olorin' Rizzi | last post by:
Hello everyone. This is quite convoluted, but I'll try to make it simple. I have a couple of bottom-line questions (I guess): 1~ what happens between the Page_Load routine in the code behind...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
2
by: Sam | last post by:
I have a custom control (MyTextBox - taken from Microsoft website) that implements the IPostBackDataHandler interface. It is added to the controls collection of a placeholder control during the...
7
by: Dan Nash | last post by:
Hi guys Ok, i have an aspx with a user control. In the user controls pageload i set a property (mystring = "test";). i then want to access that from the page_load of the aspx thats using the...
0
by: Isz | last post by:
PROBLEM: This problem is related to the postback and handling events correctly. BACKGROUND: I have a datalist which acts as a tabbes list with horizontal layout. This datalist is bound to a...
8
by: garyusenet | last post by:
I was originally taught to double click the form, to get to the load event handler and in there put anything I want to happen when my form is opened. However, today whilst reorganising some code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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.