Don't think that would matter. Isn't it more logical to add this code to the
SelectIndexChanged event of the dropdownlist? We've included our test code,
it's C# but maybe it's of use anyway to you.
Kind regards,
Nikander & Margriet Bruggeman
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ng.LoadControls
{
/// <summary>
/// Summary description for LoadControl.
/// </summary>
public class LoadControl : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
DropDownList1.Items.Add("test");
DropDownList1.Items.Add("test2");
DropDownList1.Items.Add("test3");
//AddControls(3);
}
private void AddControls(int intNumber)
{
for ( int i = 0; i < intNumber; i++ )
{
Control objControl = LoadControl("MyControl.ascx");
this.Controls.Add(objControl);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new
System.EventHandler(this.DropDownList1_SelectedInd exChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
AddControls(10);
}
}
}
"Tim::.." wrote:
The PopulateDDLNoMobAccounts is called on page load!
Maybe I have to seperate to sub into two different sub??? What do you think!
Thanks
"Nikander & Margriet Bruggeman" wrote:
So when is PopulateDDLNoMobAccounts actually called? Because the code itself
works. We tried this in C#:
Control objControl = LoadControl("MyControl.ascx");
this.Controls.Add(objControl);
Kind regards,
Nikander & Margriet Bruggeman
"Tim::.." wrote:
Can someone please tell my why the following doesn't work!
I don't get any errors on the page but when I choose a value from the
dropdownlist what is ment to happen is that a user control should be loaded
into
the page!
However I don't get any reaction what so ever!
Please see comments below!
Thanks
.. code
' loads contol according to the value given to NoMobilePhones()
Public Sub NoMobilePhones(ByVal v_intNoMobilePhoneNumbers As Integer)
Dim iCounter As Integer
If v_intNoMobilePhoneNumbers <> 0 Then
For iCounter = 1 To v_intNoMobilePhoneNumbers
Dim c1 As Control = LoadControl("AddMob.ascx")
AddHere.Controls.Add(c1)
Next
End If
End Sub
'Populates the dropdownlist with numbers 1 - 10
Public Sub PopulateDDLNoMobAccounts()
'assuming you have a button named ForLoop
Dim i As Integer
For i = 1 To 10
v_intNoMobilePhoneNumbers.Items.Add(New ListItem(i.ToString(),
i.ToString()))
Next i
' Adds a "select one" to the dropdownlist
v_intNoMobilePhoneNumbers.Items.Insert(0, New ListItem("Select One",
"0"))
v_intNoMobilePhoneNumbers.SelectedIndex = 0
' This should provide a value to the sub NoMobilePhones()
If i <> 0 Then
Dim NoAccounts As Integer
NoAccounts = CType(v_intNoMobilePhoneNumbers.SelectedItem.Value ,
Integer)
If NoAccounts = 0 Then
lblMobPhone.Text = "Please choose number of Mobile Accounts"
Else
NoMobilePhones(NoAccounts)
End If
End If
End Sub