dynamic controls

P

Paul W

i have some dynamic controls that i add to a webform (actually, i dynamiclly
build a table and add the controls to cells). some of these controls are
HtmlSelect controls with the multiple property set to true.
i have figured out how to loop through the controls on the form and find the
one i am looking for. when i go to get the values out of it, it only gives
me the fist value. for instance, if i have "a", "b", and "c", when i do the
loop through arr2 (see code below), it only has a length of 1 and it only
has the first value in the list. I need to get to all values in the
htmlselect control. how can i do this? thanks. //Paul

private void btnSaveAs_Click(object sender, System.EventArgs e)
{
....... other code
int loop1, loop2;
string paramValues = '";
System.Collections.Specialized.NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.Params;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
string s = arr1[loop1];
if(s == "cmbMediaType")
{
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
paramValues += arr2[loop2] + "|";
//Response.Write("Value " + loop2 + ": " + arr2[loop2] +
"<br>");
}
}
}
..... other code
}
 
B

Brock Allen

I'm missing what you're trying to do. But from looking at your code, in general,
you don't need to access the Request object to look at the posted data. Instead
use the server control itself. By ignoring the server controls you are really
not fully utilizing the entire object model of ASP.NET.
 
P

Paul W

well, the issue is that I am creating the controls dynamically, so I dont
have access to them in the traditional way.
in other words when i add a control i (well, actually the developer before
me) do something like:

HtmlTableCell tc;
HtmlTableRow tr;
HtmlSelect selMulti;
....init code is here
tc = new HtmlTableCell();
tc.Width = "10%";
selMulti = new HtmlSelect();
selMulti.Multiple = true;
selMulti.ID = classPopulatedFromDB.ControlName;
selMulti.Items.Capacity = 10;
selMulti.Size = 3;
tc.Controls.Add(PopulateMultiSelectControl(selMulti));
tr.Cells.Add(tc);
this.tableAddedAtDesignTime.Rows.Add(tr);

that is kinda what is going on in my code. So when I am in the code, I dont
even know the name of the control...so I cant do
this.ControlName.Items.....you get the point I hope. I understand that this
may not be the most elagant way of doing things, but it is the code I have
been handed and that I have to make work :) I hope this helps my post make
more sense as to what I am trying to accomplish.


Brock Allen said:
I'm missing what you're trying to do. But from looking at your code, in
general, you don't need to access the Request object to look at the posted
data. Instead use the server control itself. By ignoring the server
controls you are really not fully utilizing the entire object model of
ASP.NET.



i have some dynamic controls that i add to a webform (actually, i
dynamiclly
build a table and add the controls to cells). some of these controls
are
HtmlSelect controls with the multiple property set to true.
i have figured out how to loop through the controls on the form and
find the
one i am looking for. when i go to get the values out of it, it only
gives
me the fist value. for instance, if i have "a", "b", and "c", when i
do the
loop through arr2 (see code below), it only has a length of 1 and it
only
has the first value in the list. I need to get to all values in the
htmlselect control. how can i do this? thanks. //Paul
private void btnSaveAs_Click(object sender, System.EventArgs e)
{
...... other code
int loop1, loop2;
string paramValues = '";
System.Collections.Specialized.NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.Params;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
string s = arr1[loop1];
if(s == "cmbMediaType")
{
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
paramValues += arr2[loop2] + "|";
//Response.Write("Value " + loop2 + ": " + arr2[loop2] +
"<br>");
}
}
}
.... other code
}
 
B

Brock Allen

Upon postback you should always recreate them (in Page_Load or override CreateChildControls).
Then you can use FindControl to locate them. I have a post here that shows
a sample:

http://groups-beta.google.com/group...450?q=brock+dynamic+controls+viewstate&rnum=1




well, the issue is that I am creating the controls dynamically, so I
dont
have access to them in the traditional way.
in other words when i add a control i (well, actually the developer
before
me) do something like:
HtmlTableCell tc;
HtmlTableRow tr;
HtmlSelect selMulti;
...init code is here
tc = new HtmlTableCell();
tc.Width = "10%";
selMulti = new HtmlSelect();
selMulti.Multiple = true;
selMulti.ID = classPopulatedFromDB.ControlName;
selMulti.Items.Capacity = 10;
selMulti.Size = 3;
tc.Controls.Add(PopulateMultiSelectControl(selMulti));
tr.Cells.Add(tc);
this.tableAddedAtDesignTime.Rows.Add(tr);
that is kinda what is going on in my code. So when I am in the code,
I dont even know the name of the control...so I cant do
this.ControlName.Items.....you get the point I hope. I understand
that this may not be the most elagant way of doing things, but it is
the code I have been handed and that I have to make work :) I hope
this helps my post make more sense as to what I am trying to
accomplish.

I'm missing what you're trying to do. But from looking at your code,
in
general, you don't need to access the Request object to look at the
posted
data. Instead use the server control itself. By ignoring the server
controls you are really not fully utilizing the entire object model
of
ASP.NET.
i have some dynamic controls that i add to a webform (actually, i
dynamiclly
build a table and add the controls to cells). some of these
controls
are
HtmlSelect controls with the multiple property set to true.
i have figured out how to loop through the controls on the form and
find the
one i am looking for. when i go to get the values out of it, it
only
gives
me the fist value. for instance, if i have "a", "b", and "c", when
i
do the
loop through arr2 (see code below), it only has a length of 1 and it
only
has the first value in the list. I need to get to all values in the
htmlselect control. how can i do this? thanks. //Paul
private void btnSaveAs_Click(object sender, System.EventArgs e)
{
...... other code
int loop1, loop2;
string paramValues = '";
System.Collections.Specialized.NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.Params;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
string s = arr1[loop1];
if(s == "cmbMediaType")
{
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
paramValues += arr2[loop2] + "|";
//Response.Write("Value " + loop2 + ": " + arr2[loop2] +
"<br>");
}
}
}
.... other code
}
 
P

Paul W

dang. i was hoping to avoid that because it makes this code even more
complicated :) thank you for the link. i think it will solve the problem.
Brock Allen said:
Upon postback you should always recreate them (in Page_Load or override
CreateChildControls). Then you can use FindControl to locate them. I have
a post here that shows a sample:

http://groups-beta.google.com/group...450?q=brock+dynamic+controls+viewstate&rnum=1




well, the issue is that I am creating the controls dynamically, so I
dont
have access to them in the traditional way.
in other words when i add a control i (well, actually the developer
before
me) do something like:
HtmlTableCell tc;
HtmlTableRow tr;
HtmlSelect selMulti;
...init code is here
tc = new HtmlTableCell();
tc.Width = "10%";
selMulti = new HtmlSelect();
selMulti.Multiple = true;
selMulti.ID = classPopulatedFromDB.ControlName;
selMulti.Items.Capacity = 10;
selMulti.Size = 3;
tc.Controls.Add(PopulateMultiSelectControl(selMulti));
tr.Cells.Add(tc);
this.tableAddedAtDesignTime.Rows.Add(tr);
that is kinda what is going on in my code. So when I am in the code,
I dont even know the name of the control...so I cant do
this.ControlName.Items.....you get the point I hope. I understand
that this may not be the most elagant way of doing things, but it is
the code I have been handed and that I have to make work :) I hope
this helps my post make more sense as to what I am trying to
accomplish.

I'm missing what you're trying to do. But from looking at your code,
in
general, you don't need to access the Request object to look at the
posted
data. Instead use the server control itself. By ignoring the server
controls you are really not fully utilizing the entire object model
of
ASP.NET.

i have some dynamic controls that i add to a webform (actually, i
dynamiclly
build a table and add the controls to cells). some of these
controls
are
HtmlSelect controls with the multiple property set to true.
i have figured out how to loop through the controls on the form and
find the
one i am looking for. when i go to get the values out of it, it
only
gives
me the fist value. for instance, if i have "a", "b", and "c", when
i
do the
loop through arr2 (see code below), it only has a length of 1 and it
only
has the first value in the list. I need to get to all values in the
htmlselect control. how can i do this? thanks. //Paul
private void btnSaveAs_Click(object sender, System.EventArgs e)
{
...... other code
int loop1, loop2;
string paramValues = '";
System.Collections.Specialized.NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.Params;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
string s = arr1[loop1];
if(s == "cmbMediaType")
{
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
paramValues += arr2[loop2] + "|";
//Response.Write("Value " + loop2 + ": " + arr2[loop2] +
"<br>");
}
}
}
.... other code
}
 

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,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top