Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

Need to dynamically create controls base on dropdownlist selection
Ronald Thomas posted at Tuesday, November 03, 2009 12:59 PM

I wish to allow the user to determine how many controls to generate based on their selection in the dropdownlist. My issue is that the value selected from the dropdownlist is available only after onselectedindexchanged is triggered. So I getting the value after the screen is rendered. Is there a way to obtain the selected value before onselectedindexchanged is triggered. The current code is generating the dynamic controls but only after I select a different item in the dropdownlist. Any HELP would be greatly appreciated!!! Here is my code snippets:

ASPX:

<form id="form1" runat="server">

&nbsp;Number of Children controls?&nbsp; &nbsp;

<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="True" EnableViewState="true" onselectedindexchanged="DDL1_SelectedIndexChanged"

>

<asp:ListItem>0</asp:ListItem>

<asp:ListItem>1</asp:ListItem>

<asp:ListItem>2</asp:ListItem>

<asp:ListItem>3</asp:ListItem>

<asp:ListItem>4</asp:ListItem>

<asp:ListItem>5</asp:ListItem>

<asp:ListItem>6</asp:ListItem>

<asp:ListItem>7</asp:ListItem>

<asp:ListItem>8</asp:ListItem>

<asp:ListItem>9</asp:ListItem>

<asp:ListItem>10</asp:ListItem>

</asp:DropDownList>

<br />

<div>

&nbsp;<br />

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:PlaceHolder ID="myPlaceHolder1" runat="server"/>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTrigger ControlID="DDL1" EventName="SelectedIndexChanged" />

</Triggers>

</asp:UpdatePanel>

</div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

</form>

 

ASPX.CS

 

protected override void OnInit(EventArgs e)

{

            base.OnInit(e);

            CreateChildControls();

}

 

protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)

{

            myCount = DDL1.SelectedIndex;   // myCount represents the # of rows of dynamic textboxes

}

public void CreateChildControls()

{

             for (int r = 0; r < myCount; r++)

            {

                        Child.DynamicTB = new TextBox[myCount, 6];

                        for (int c = 0; c < 6; c++)

                       {

                                TextBox textbox = new TextBox();

                                textbox.ID = "myTextbox" + r.ToString() + c.ToString();

                                myPlaceHolder1.Controls.Add(textbox);

                                Child.DynamicTB[r, c] = textbox;

                        }

            }

}           

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
This article deals with this exact scenario
Robbe Morris replied to Ronald Thomas on Wednesday, November 04, 2009 12:52 PM

See if it doesn't help you out.

http://www.eggheadcafe.com/articles/extendtextboxviewstate.asp

Reply    Reply Using Power Editor
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0