When a button is clicked, I want to populate multiple ddl's and under
certain circumstances I want to set the SelectedValue. I've tried different
variations of this:
protected void btnImport_Click(object sender, EventArgs e)
{
[snip]
int tblcols = dsCsv.Tables["csv"].Columns.Count;
for(int i=0; i<tblcols; i++)
{
ListItem liItem = new ListItem();
liItem.Text = dsCsv.Tables["csv"].Columns[i].ToString();
liItem.Value = dsCsv.Tables["csv"].Columns[i].ToString();
ddlAddress.Items.Add(liItem);
ddlOrganization.Items.Add(liItem);
if (liItem.Text.ToLower().IndexOf("company") >=0)
{
ddlOrganization.Items.FindByText(liItem.Text).Selected = true;
}
}
}
This doesn't work. Although items were added, the code won't recognize
these items until the entire btnImport_Click is finished. How can I
accomplish this?
Thanks.Sharing ListItems is a bad idea. The problem is that Selected is a property
of an ListItem rather than the ddl the item belongs to. It means that as
soon as you select an item in one list, it will become selected also in
another list.
Create separate instances if ListItems for every list.
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"J" <nobody@.nowhere.com> wrote in message
news:13ufgs5682locff@.corp.supernews.com...
> When a button is clicked, I want to populate multiple ddl's and under
> certain circumstances I want to set the SelectedValue. I've tried
> different variations of this:
> protected void btnImport_Click(object sender, EventArgs e)
> {
> [snip]
> int tblcols = dsCsv.Tables["csv"].Columns.Count;
> for(int i=0; i<tblcols; i++)
> {
> ListItem liItem = new ListItem();
> liItem.Text = dsCsv.Tables["csv"].Columns[i].ToString();
> liItem.Value = dsCsv.Tables["csv"].Columns[i].ToString();
> ddlAddress.Items.Add(liItem);
> ddlOrganization.Items.Add(liItem);
> if (liItem.Text.ToLower().IndexOf("company") >=0)
> {
> ddlOrganization.Items.FindByText(liItem.Text).Selected = true;
> }
> }
> }
> This doesn't work. Although items were added, the code won't recognize
> these items until the entire btnImport_Click is finished. How can I
> accomplish this?
> Thanks.
>
Ah, this bit of info solves my issues. Thanks.
"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@.mMvVpPsS.org> wrote in
message news:%23GemqocjIHA.4376@.TK2MSFTNGP05.phx.gbl...
> Sharing ListItems is a bad idea. The problem is that Selected is a
> property of an ListItem rather than the ddl the item belongs to. It means
> that as soon as you select an item in one list, it will become selected
> also in another list.
> Create separate instances if ListItems for every list.
> --
> Eliyahu Goldin,
> Software Developer
> Microsoft MVP [ASP.NET]
> http://msmvps.com/blogs/egoldin
> http://usableasp.net
>
> "J" <nobody@.nowhere.com> wrote in message
> news:13ufgs5682locff@.corp.supernews.com...
>
Friday, March 16, 2012
populating ddl and setting selectedvalue
Labels:
asp,
button,
circumstances,
clicked,
ddl,
multiple,
net,
populate,
populating,
selectedvalue,
setting,
undercertain
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment