Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Problem with Dynamically added dropdownlists to placeholders, panels, tables, etc.

Reply
Thread Tools

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

 
 
ChuckSlayer
Guest
Posts: n/a
 
      02-09-2007
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[i];
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.ModuleM essageType.RedError,
this);
OnDnnMsgEvent(msgArgs);
}
}
---------------- End Code Behind (Page
Load)-----------------------------

 
Reply With Quote
 
 
 
 
ChuckSlayer
Guest
Posts: n/a
 
      02-10-2007
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>

<asplaceHolder 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[i] = ddlArray[i].selectedIndex;}
}
for(i = 0; i < userSelectionArray.length; i++)
{
if(userSelectionArray[i] > 0)
{
for(j = 0; j < ddlArray.length; j++)
{
ddlArray[j][userSelectionArray[i]].disabled =
"disabled";
}
}
}
}
</script>
<script type="text/javascript">
function toggleDropDowns( justSelected, id )
{
if(userSelectionArray[id] != 0)
{
for(i = 0; i < ddlArray.length; i++)
{
ddlArray[i][userSelectionArray[id]].disabled = "";
}
}

userSelectionArray[id] = justSelected.selectedIndex;

if(userSelectionArray[id] != 0)
{
for(i = 0; i < ddlArray.length; i++)
{
ddlArray[i][userSelectionArray[id]].disabled = "disabled";
}
}
}
</script>
<script type="text/javascript">
function enableDDLs()
{
for(i = 0; i < ddlArray.length; i++)
{
for(j = 0; j < ddlArray[i].length; j++)
{
ddlArray[i][j].disabled = "";
}
}
}
</script>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting attributes for dynamically created dropdownlists shivam ASP .Net 0 08-20-2007 09:16 AM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
DropDownLists in DataGrids-- can I dynamically name them? Jim Bancroft ASP .Net 3 12-20-2004 03:52 PM
how come user control added to page doesn't get added to codebehind file? Bennett Haselton ASP .Net 1 11-08-2004 09:26 PM
SelectedIndex to dynamically created DropDownLists falk ASP .Net Web Controls 0 09-02-2004 04:39 AM



Advertisments