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