English 中文(简体)
How to get the selected value from ajax.net ComboBox through javascript
原标题:

I need to get the selected value from an ajax.net combobox throught javascript so that I can do some client side validation.

What s the best way to do this? Thanks,


I ve been able to get the value with this:

var combo = $get( ddlComarcas );
var comboHidden = $get( ddlComarcas_HiddenField );
var o4 = combo.getElementsByTagName( li )[comboHidden.value].childNodes[0];

alert( "  + o4.data +  " );

But I still need to trim the value from o4.data. Anyone can point how to do that visual studio 2008 jquery?

问题回答

You can use jQuery or just use DOM:

jQuery:

var selection = $( #selectID ).val();

DOM:

var selection = document.getElementById("selectID").value;

asp.net -> server side

javascript -> client side

I think the answer is value doesnt exist client side so it cant be retrieved. There are easier ways to get index tho (assuming whatever initialization is complete).

selected index:         $find("<%=cboName.ClientID%>").get_hiddenFieldControl().value;
selected index (again): $find("<%=cboName.ClientID%>").get_selectedIndex();
selected text:          $find("<%=cboName.ClientID%>").get_textBoxControl().value;

As far as I can tell, validating a combobox on the client requires some faith in index or text, or some kind of server side workaround.

To provide a direct answer to the subject line, a javascript array could be created server side with each combobox value and then referenced client side by selected index...

codebehind:

 // write combobox values to asp:literal
 foreach (ListItem i in cboName.Items)
         litCboValues.Text += """ + i.Value.Replace(""", "\"") + "", ";
 litCboValues.Text = litCboValues.Text.TrimEnd(new char[] { , ,    });

aspx:

<script>
// array of values
 var cboValues = [ <asp:Literal id="litCboValues" runat="server" /> ];

// add an alert to the combobox to test
function pageLoad()
{
  $find("<%=cboName.ClientID%>").get_textBoxControl().onblur = function () { 
    alert( cboValues[$find("<%=cboName.ClientID%>").get_selectedIndex()] );
  };
}
</script>


<asp:ComboBox id="cboName" runat="server" ...

this works (today) in IE and Chrome - about the only thing ie is good for is the debugger f12 (- you can browse through the watched objects

Following // i do it on button but you could probably do it on a combo event Follow
 function addFollowed() {
      var combo = $get( <%= FollowListBox.ClientID %> ); 
      var toFollow = combo.control._textBoxControl.value;




相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签