Problem with Dynamically added dropdownlists to placeholders, panels, tables, etc.

Discussion in 'ASP .Net Web Controls' started by ChuckSlayer, Feb 9, 2007.

  1. ChuckSlayer

    ChuckSlayer Guest

    Can anyone please explain to me why this does not work and how should
    I fix it?. I am adding dropdownlists dynamically on the code behind.
    These dropdownlists are set not to use autopost back. After the user
    is done selecting all desired values for all dropdownlists, they click
    on the next button and the event handler is supposed to read their
    values. My problem is that it is not working. When the event handler
    reads all dropdownlists, their selected index have been reseted to 0
    and their view state is lost.

    This is my current code which i am attempting to do with a ASP:Table
    (I tried with panels and placeholders and got the same result)

    -------------Design View ------------------------------

    <fieldset id="import">
    <legend>Pre-Import</legend>
    The following headers were found on the file you provided.<br />
    Please map your headers with the headers used on this version of
    TreeFrog<br />
    <br />

    <asp:Table id="tbUser" GridLines="Both" HorizontalAlign="Center"
    Font-Names="Verdana" CellPadding="0" CellSpacing="0" Runat="server"/>

    <br />
    <cc:ServerImageButton ID="ibProceedEnabled" runat="server"
    Text="Proceed" ButtonType="ImportContacts" IncludeDiv="false"
    OnClick="cmdImportContacts"/>
    <cc:ServerImageButton ID="ibCancel" runat="server" Text="Cancel"
    ButtonType="cancel" IncludeDiv="false" OnClick="cmdCancelExit"/>
    </fieldset>

    ----------------------End Design View
    --------------------------------------------

    ------------------- Code Behind (Page Load)
    ------------------------------
    protected void Page_Init(object sender, EventArgs e)
    {
    try
    {
    if (!IsPostBack)
    {
    cmdCancelExit(sender, e);
    return;
    }

    userHeaders = GetHeaders(MyFileContent);
    validHeaders = GetValidHeader();

    for (int i = 0; i<userHeaders.Count; i++)
    {
    trUser = new TableRow();

    tcUser = new TableCell();
    tcUser.Text = userHeaders;
    trUser.Controls.Add(tcUser);

    DropDownList ddlHtml = new DropDownList();
    ddlHtml.ID = "ddlValidHeaders_" + i;
    ddlHtml.DataSource = validHeaders;
    ddlHtml.DataBind();
    ListItem li = new ListItem("None", "");
    ddlHtml.Items.Insert(0,li);

    tcUser = new TableCell();
    tcUser.Controls.Add(ddlHtml);
    trUser.Controls.Add(tcUser);

    tbUser.Controls.Add(trUser);

    ddlHtml.Attributes.Add("onchange",
    "toggleDropDowns(this,'" + ddlHtml.ClientID + "'," + userHeaders.Count
    + "," + validHeaders.Count + ");");
    }
    }
    catch (Exception exc)
    {
    DnnMsgEventArgs msgArgs = new
    DnnMsgEventArgs(exc.Message,
    DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError,
    this);
    OnDnnMsgEvent(msgArgs);
    }
    }
    ---------------- End Code Behind (Page
    Load)-----------------------------
     
    ChuckSlayer, Feb 9, 2007
    #1
    1. Advertising

  2. ChuckSlayer

    ChuckSlayer Guest

    In case anyone wonders how I resolved this problem, here is the link i
    found most useful.

    http://aspnet.4guysfromrolla.com/articles/012506-1.aspx


    <script type="text/javascript">
    var userSelectionArray = null;
    var ddlArray = new Array();
    </script>

    <asp:placeHolder ID="phInitializeDDLArray" runat="server"/> //Through
    this place holder I initialized the values for the ddlArray. Since I
    am working with DNN 4.4.0, the ClientIDs of all my controls was a mess
    with all the added text DNN adds to them. I had to retrieve the
    ClientIDs after I had done the DataBind to each dropdown. I colleted
    their ids on the event OnDataBound and put them on the placeholder as
    javascripts.

    <script type="text/javascript">

    setDDLs();
    function setDDLs()
    {
    if(userSelectionArray == null)
    {
    userSelectionArray = new Array(ddlArray.length);
    for(i = 0; i < ddlArray.length; i++)
    {userSelectionArray = ddlArray.selectedIndex;}
    }
    for(i = 0; i < userSelectionArray.length; i++)
    {
    if(userSelectionArray > 0)
    {
    for(j = 0; j < ddlArray.length; j++)
    {
    ddlArray[j][userSelectionArray].disabled =
    "disabled";
    }
    }
    }
    }
    </script>
    <script type="text/javascript">
    function toggleDropDowns( justSelected, id )
    {
    if(userSelectionArray[id] != 0)
    {
    for(i = 0; i < ddlArray.length; i++)
    {
    ddlArray[userSelectionArray[id]].disabled = "";
    }
    }

    userSelectionArray[id] = justSelected.selectedIndex;

    if(userSelectionArray[id] != 0)
    {
    for(i = 0; i < ddlArray.length; i++)
    {
    ddlArray[userSelectionArray[id]].disabled = "disabled";
    }
    }
    }
    </script>
    <script type="text/javascript">
    function enableDDLs()
    {
    for(i = 0; i < ddlArray.length; i++)
    {
    for(j = 0; j < ddlArray.length; j++)
    {
    ddlArray[j].disabled = "";
    }
    }
    }
    </script>
     
    ChuckSlayer, Feb 10, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josh
    Replies:
    1
    Views:
    9,553
    Jeffrey Palermo [MCP]
    May 26, 2004
  2. Sinisa Ruzin
    Replies:
    0
    Views:
    865
    Sinisa Ruzin
    Oct 7, 2004
  3. =?Utf-8?B?cm9kY2hhcg==?=

    Panels, DropDownLists,RadioButtons and Bears

    =?Utf-8?B?cm9kY2hhcg==?=, Aug 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    352
    =?Utf-8?B?cm9kY2hhcg==?=
    Aug 21, 2006
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    411
    Fredrik Lundh
    Aug 13, 2008
  5. Josh
    Replies:
    1
    Views:
    204
Loading...

Share This Page