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

C

ChuckSlayer

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)-----------------------------
 
C

ChuckSlayer

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>
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top