tags:

views:

68

answers:

2

EDIT2:

I tried something new I added this line of code:

$(self.ddlOwner + "option[value='" + this.owner_USER_ID +"']").attr("selected", "selected");

However this line of code does in fact set the value correctly but it does so on all the DDLs on the page. So if the value is 1 all four DDLs I have on the entire page set the value = 1.

End Edit2

I have a custom control DropDownList.

public class AssigneesDDL : System.Web.UI.WebControls.DropDownList
    {
    ...
    }

It gets populated onInit. And from the server side I would use a property to set its value.

this property:

 public string ValueID
        {
            get { return base.SelectedItem.Value; }

            set
            {
                ListItem li = base.Items.FindByValue(value.ToString());
                if (li != null)
                {
                    li.Selected = true;
                }
                else
                {
                    ListItem li2 = new ListItem("", "");
                    base.Items.Add(li2);
                    li2.Selected = true;
                }
            }

        }

Now I need to select the value from the client side and I am trying to do so with some JQuery, without any luck.

I have the Value captured but no idea how to set it. I've tried both:

 $(self.ddlOwner).val(this.owner_USER_ID);

and

$(self.ddlOwner + "option:selected").val(this.owner_USER_ID);

Anybody have any ideas?

EDIT:

Oh and I've tried:

$(self.ddlOwner + "option:selected").text(this.owner_USER_ID);

this one actually adds a new line into the DDL with the right value, but it also adds that value to every DDL on my page. Which is odd. Not really sure why it does that.

Self.ddlOwner is ("#" + ddl.clientID). So I have the correct handle on the control.

A: 

If you have a custom control that gets populated on the server side why are you trying to set the value on the client side? It should be enough to set the SelectedItem or SelectedIndex property, and then it will be rendered correctly.

Can you please explain your scenario more?

codemonkeh
A: 

Apparently the issue was that I was using web control. An ascx. When I moved to an external control then the .val() method worked fine.

Not sure why it wouldn't work with a DDL from a ascx web control.

Collin Estes