开发者

Dropdown SelectedValue changes unrelated properties

 drpDwnLstBillContact.SelectedValue = billContactId.ToString();
 drpDwnLstRegContact.SelectedValue = regContactId.ToString();
 drpDwnLstTechContact.SelectedValue = techContactId.ToString();

I am trying to set dropdowns' selectedvalue property, but I have a problem.

The values of variables are:

billContactId=786867;
re开发者_如何学JAVAgContactId=3487347;
techContactId=37463;

The problem is on the first line. billContactId is assigned to selectedvalue property of drpDwnLstBillContact, also on the second line regContactId is assigned to drpDwnLstRegContact's selectedvalue property. But when it is assigned to it the first dropdown's (drpDwnLstBillContact), selected value is also set to regContactId too. Why that second line of code effects the first line?


I found the problem and the solution.

In the FillDropDowns method I have created only one ListItem and added it to each dropdown, so when I change the dropdowns selectedvalue property it changes the list item and all of dropdowns' listitem change too. Now I have created seperate ListItem objects for each dropdown and problem is solved.

Old Code

 foreach (string[] contactData in data)
        {
            ListItem li = new ListItem(contactData[0], contactData[1]);

            drpDwnLstRegContact.Items.Add(li);
            drpDwnLstTechContact.Items.Add(li);
            drpDwnLstBillContact.Items.Add(li);
        }

New Code

 foreach (string[] contactData in data)
        {
            ListItem li = new ListItem(contactData[0], contactData[1]);
            ListItem li1 = new ListItem(contactData[0], contactData[1]);
            ListItem li2 = new ListItem(contactData[0], contactData[1]);
            drpDwnLstRegContact.Items.Add(li);
            drpDwnLstTechContact.Items.Add(li1);
            drpDwnLstBillContact.Items.Add(li2);
        }


It won't affect the first; drop down lists are atomic and I've never heard of this issue before. Are you sure somewhere else in code the control isn't being reassigned to a different value?


I cant clearly get what's the problem. However I guess you are filling your DropDownList on every Page.Load event which makes the first item always selected.

Try the following on Page.Load handler:

if (!Page.IsPostBack)
{
    FillDropDownList();
}


This can help. (Don't ask why...)

drpDwnLstBillContact.SelectedIndex = -1;
drpDwnLstBillContact.SelectedIndex = 
    drpDwnLstBillContact.Items.IndexOf(drpDwnLstBillContact.Items.
        FindByValue(billContactId.ToString()));

drpDwnLstRegContact.SelectedIndex = -1;
drpDwnLstRegContact.SelectedIndex = 
    drpDwnLstRegContact.Items.IndexOf(drpDwnLstRegContact.Items.
        FindByValue(regContactId.ToString()));

drpDwnLstTechContact.SelectedIndex = -1;
drpDwnLstTechContact.SelectedIndex = 
    drpDwnLstTechContact.Items.IndexOf(drpDwnLstTechContact.Items.
        FindByValue(techContactId.ToString()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜