473,386 Members | 1,883 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,386 software developers and data experts.

count dynamic controls

Hi,

In ASP.net 2.0 I make a control which add the same controls dynamically. In
the oninit event I add the controls to the controls collection. After that
the loadviewstate event fills in the information on postbacks. The control
can add and delete controls that is why on the postback I don't know how many
controls there are.

At the moment I am able to get the controls rendering but I have problems to
save the count of the dynamic controls. I don't know where I can save the
number of controls. For the moment I have saved the count in Sessionstate but
that is not what I want.

I have tried to save it in controlstate but that does not work because in
the oninit I cannot use it.

My question is what is the best way to save the count of the controls.
Greetings,

JP
Dec 28 '05 #1
8 2076
Are you talking about storing just a number? Try storing it in viewstate.

"novus" <no***@community.nospam> wrote in message
news:5F**********************************@microsof t.com...
Hi,

In ASP.net 2.0 I make a control which add the same controls dynamically.
In
the oninit event I add the controls to the controls collection. After that
the loadviewstate event fills in the information on postbacks. The control
can add and delete controls that is why on the postback I don't know how
many
controls there are.

At the moment I am able to get the controls rendering but I have problems
to
save the count of the dynamic controls. I don't know where I can save the
number of controls. For the moment I have saved the count in Sessionstate
but
that is not what I want.

I have tried to save it in controlstate but that does not work because in
the oninit I cannot use it.

My question is what is the best way to save the count of the controls.
Greetings,

JP

Dec 28 '05 #2
In viewstate is not possible because it is not available in the onint event.

"Marina" wrote:
Are you talking about storing just a number? Try storing it in viewstate.

"novus" <no***@community.nospam> wrote in message
news:5F**********************************@microsof t.com...
Hi,

In ASP.net 2.0 I make a control which add the same controls dynamically.
In
the oninit event I add the controls to the controls collection. After that
the loadviewstate event fills in the information on postbacks. The control
can add and delete controls that is why on the postback I don't know how
many
controls there are.

At the moment I am able to get the controls rendering but I have problems
to
save the count of the dynamic controls. I don't know where I can save the
number of controls. For the moment I have saved the count in Sessionstate
but
that is not what I want.

I have tried to save it in controlstate but that does not work because in
the oninit I cannot use it.

My question is what is the best way to save the count of the controls.
Greetings,

JP


Dec 28 '05 #3
You can declare a page level variable, set it in oninit, and then set the
viewstate in page_load with whatever is in the variable. You have to take
things one step further sometimes.

"novus" <no***@community.nospam> wrote in message
news:4C**********************************@microsof t.com...
In viewstate is not possible because it is not available in the onint
event.

"Marina" wrote:
Are you talking about storing just a number? Try storing it in viewstate.

"novus" <no***@community.nospam> wrote in message
news:5F**********************************@microsof t.com...
> Hi,
>
> In ASP.net 2.0 I make a control which add the same controls
> dynamically.
> In
> the oninit event I add the controls to the controls collection. After
> that
> the loadviewstate event fills in the information on postbacks. The
> control
> can add and delete controls that is why on the postback I don't know
> how
> many
> controls there are.
>
> At the moment I am able to get the controls rendering but I have
> problems
> to
> save the count of the dynamic controls. I don't know where I can save
> the
> number of controls. For the moment I have saved the count in
> Sessionstate
> but
> that is not what I want.
>
> I have tried to save it in controlstate but that does not work because
> in
> the oninit I cannot use it.
>
> My question is what is the best way to save the count of the controls.
>
>
> Greetings,
>
> JP


Dec 28 '05 #4
Sorry Marina, but I think you don't understand the problem. Please, you have
to take things one step further before you answer ;-)

I will try too clarify the problem.
1. Controls must be added/recreated to the Controls collection in the
OnInit event for each postback.
2. In the LoadViewState event the dynamically added controls will be
filled with the state values from the ViewState automatically (default
asp.net behaviour). This will only work if all controls are added before the
LoadViewState event fires.

Because my control allow the user to dynamically add or delete controls I
don't know how many controls I have to recreate at postback in the OnInit
event. It’s possible to store a count in the SessionState but that’s not a
very elegant solution. So I’m searching for something like Control- or
ViewState that’s available in the OnInit event.

The OnInit event of my control looks like:

protected override void OnInit(EventArgs e)
{
// Add the add button to the controls collection.
moAddButton.Text = "Add";
this.Controls.Add(moAddButton);
moAddButton.Click += new EventHandler(moAddButton_Click);
// Add all multi item controls here so the viewstate will be
restored correctly.
for (int iIndex = 1; iIndex <= Count; iIndex++)
{
AddItem(iIndex);
}
base.OnInit(e);
}

private void AddItem(int viIndex)
{
// Create a new multi item control.
MultiItem oMultiItem = new MultiItem();
oMultiItem.ID = "itmMultiItem" + viIndex.ToString();
this.Controls.Add(oMultiItem);
// Add a textbox to the multi item.
TextBox oTextBox = new TextBox();
oTextBox.ID = "txtTextBox";
oMultiItem.Controls.Add(oTextBox);
// Add a delete button to the multi item.
Button oDeleteButton = new Button();
oDeleteButton.ID = "cmdDelete";
oDeleteButton.Text = "Delete";
oMultiItem.Controls.Add(oDeleteButton);
oDeleteButton.Click += new EventHandler(oDeleteButton_Click);
}

private string CountStateKey
{
get
{
return this.UniqueID + "Count";
}
}

private int Count
{
get
{
// Retrieve the count from session state.
if (Context.Session[CountStateKey] != null)
{
return (int)Context.Session[CountStateKey];
}
return 0;
}
set
{
// Store the count in session state.
Context.Session[CountStateKey] = value;
}
}

"Marina" wrote:
You can declare a page level variable, set it in oninit, and then set the
viewstate in page_load with whatever is in the variable. You have to take
things one step further sometimes.

"novus" <no***@community.nospam> wrote in message
news:4C**********************************@microsof t.com...
In viewstate is not possible because it is not available in the onint
event.

"Marina" wrote:
Are you talking about storing just a number? Try storing it in viewstate.

"novus" <no***@community.nospam> wrote in message
news:5F**********************************@microsof t.com...
> Hi,
>
> In ASP.net 2.0 I make a control which add the same controls
> dynamically.
> In
> the oninit event I add the controls to the controls collection. After
> that
> the loadviewstate event fills in the information on postbacks. The
> control
> can add and delete controls that is why on the postback I don't know
> how
> many
> controls there are.
>
> At the moment I am able to get the controls rendering but I have
> problems
> to
> save the count of the dynamic controls. I don't know where I can save
> the
> number of controls. For the moment I have saved the count in
> Sessionstate
> but
> that is not what I want.
>
> I have tried to save it in controlstate but that does not work because
> in
> the oninit I cannot use it.
>
> My question is what is the best way to save the count of the controls.
>
>
> Greetings,
>
> JP


Dec 29 '05 #5
Novus,
Sorry Marina, but I think you don't understand the problem. Please, you
have
to take things one step further before you answer ;-)

It is seldom the one who answers in a newsgroup fault that he/she does not
understand the problem.

If you don't like the answer, than ignore it. I have seen more people in
this newsgroups who take first the answer that fits them because it is
inline with their question. That it than does answer their question but not
the problem is than their problem from which the learn. However, you did not
get even more answers than from Marina.

I would surely not ignore an answer from Marina by the way in these
newsgroups. I see very seldom that she is not on the right track, which can
be however always the situation in a newsgroup because of a worse described
question.

At least this following phrase of you is for me completely wizwaz.
The control can add and delete controls that is why on the postback I don't
know how many
controls there are.


How do you do that, where do you do that, on the clientside with JavaScript
on the ServerSide (than it is postedback in advance).

You are right Marina should not have answered you, because the description
of your problem does not tell what you want.

However she gave for what you asked, the best alternative for a Session.
From your current descriptions there are in my opinion no other answers to
give than Marina did.

Just my opinion,

Cor
Dec 29 '05 #6
Thanks Cor.

You know, I still don't think I understand what he is looking for. But you
are right, I just tried my best given the description of the problem.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ef**************@tk2msftngp13.phx.gbl...
Novus,
Sorry Marina, but I think you don't understand the problem. Please, you
have
to take things one step further before you answer ;-)


It is seldom the one who answers in a newsgroup fault that he/she does not
understand the problem.

If you don't like the answer, than ignore it. I have seen more people in
this newsgroups who take first the answer that fits them because it is
inline with their question. That it than does answer their question but
not the problem is than their problem from which the learn. However, you
did not get even more answers than from Marina.

I would surely not ignore an answer from Marina by the way in these
newsgroups. I see very seldom that she is not on the right track, which
can be however always the situation in a newsgroup because of a worse
described question.

At least this following phrase of you is for me completely wizwaz.
The control can add and delete controls that is why on the postback I
don't know how many
controls there are.


How do you do that, where do you do that, on the clientside with
JavaScript on the ServerSide (than it is postedback in advance).

You are right Marina should not have answered you, because the description
of your problem does not tell what you want.

However she gave for what you asked, the best alternative for a Session.
From your current descriptions there are in my opinion no other answers to
give than Marina did.

Just my opinion,

Cor

Dec 29 '05 #7
Thanks for helping anyway and Cor I agree with your writing!

I admit that it is hard to define what I want but I can tell you that it is
frustrating to work and take things steps deeper for several hours on
someting that just doesn't work :-(

Cor, the controls are added server side.

The solution I found is to add a hidden field to the control which can be
read in the oninit.

I am much more happy now, it seems to work great!

Thanx for your help,

JP
"Marina" wrote:
Thanks Cor.

You know, I still don't think I understand what he is looking for. But you
are right, I just tried my best given the description of the problem.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ef**************@tk2msftngp13.phx.gbl...
Novus,
Sorry Marina, but I think you don't understand the problem. Please, you
have
to take things one step further before you answer ;-)


It is seldom the one who answers in a newsgroup fault that he/she does not
understand the problem.

If you don't like the answer, than ignore it. I have seen more people in
this newsgroups who take first the answer that fits them because it is
inline with their question. That it than does answer their question but
not the problem is than their problem from which the learn. However, you
did not get even more answers than from Marina.

I would surely not ignore an answer from Marina by the way in these
newsgroups. I see very seldom that she is not on the right track, which
can be however always the situation in a newsgroup because of a worse
described question.

At least this following phrase of you is for me completely wizwaz.
The control can add and delete controls that is why on the postback I
don't know how many
controls there are.


How do you do that, where do you do that, on the clientside with
JavaScript on the ServerSide (than it is postedback in advance).

You are right Marina should not have answered you, because the description
of your problem does not tell what you want.

However she gave for what you asked, the best alternative for a Session.
From your current descriptions there are in my opinion no other answers to
give than Marina did.

Just my opinion,

Cor


Dec 29 '05 #8
JD
> asp.net behaviour). This will only work if all controls are added before
the
LoadViewState event fires.


If I'm understanding you right, then I would revist the above premise again
if I were you. Code:

//Code for homemade control
public class Class1:UserControl
{
public Class1()
{
Load += new EventHandler(Page_Load);
}
int _controlCount = 0;
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
_controlCount = 0;
else
_controlCount += 1;
Label L = new Label();
L.ID = "MyLabel";
L.Text = _controlCount.ToString();
Controls.Add(L);
}
protected override void LoadViewState(object savedState)
{
_controlCount = (int)savedState;
}
protected override object SaveViewState()
{
return _controlCount;
}
}

//Page that will place homemade control onto the page in the page load.
//there is also a button this page that causes a post back
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 C = new Class1();
C.ID = "MyControl";
PH.Controls.Add(C); //PH is a place holder
}
}
In my test after the control is loaded within the page load event, the
control's load viewstate is called and restored, works like I expect. I'm
working on a project right now that loads all it's controls dynamically in
the page load and it works fine.

JD
Dec 29 '05 #9

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

Similar topics

2
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted...
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...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
0
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be...
1
by: Diffident | last post by:
Hello All, I am trying to add dynamic controls onto my page and here is how I am doing that. I have a page which has a button called as "AddMoreControls" and in this button's event handler I...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
2
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.