count dynamic controls

G

Guest

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
 
M

Marina

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.
 
G

Guest

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;
}
}
 
C

Cor Ligthert [MVP]

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
 
M

Marina

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.
 
G

Guest

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
 
J

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top