English 中文(简体)
how will I call the on change event of the ajax dropdownlist?
原标题:

In my MVC application I am using an ajax dropdownlist and an ajax Cascading dropdownlist I want to write the onChange event of the cascading dropdownlist please tell me what shall I do.

I am posting the view page that I am using and the js file that creates the cascading dropdownlist.Please tell me where all the places I need to do the changes.

The view Page is as follows

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index1</title>

    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
    <script type="text/javascript" src="../../Scripts/CascadingDropDownList.js"></script>
 </head>
<body>


    <div>

    <label for="Makes">Car Make:</label>
    <%= Html.DropDownList("Makes")%>

    &nbsp;&nbsp;

    <label for="Makes">Car Model:</label>    
    <%= Html.CascadingDropDownList("Models", "Makes")%>
        <br />
        <%=Html.TextBox ("id",ViewData ["id"]) %>
    </div>
</body>
</html>

The javascript where the cascading dropdown list is being formed:

public static class JavaScriptExtensions
{
    public static string CascadingDropDownList(this HtmlHelper helper, string name, string associatedDropDownList)
    {
        var sb = new StringBuilder();

        // render select tag
        sb.AppendFormat("<select name= {0}  id= {0} ></select>", name);
        sb.AppendLine();

        // render data array
        sb.AppendLine("<script type= text/javascript >");
        var data = (CascadingSelectList)helper.ViewDataContainer.ViewData[name];
        var listItems = data.GetListItems();
        var colArray = new List<string>();
        foreach (var item in listItems)
            colArray.Add(String.Format("{{key: {0} ,value: {1} ,text: {2} }}", item.Key, item.Value, item.Text));
        var jsArray = String.Join(",", colArray.ToArray());
        sb.AppendFormat("$get( {0} ).allOptions=[{1}];", name, jsArray);
        sb.AppendLine();
        sb.AppendFormat("$addHandler($get( {0} ),  change , Function.createCallback(bindDropDownList, $get( {1} )));", associatedDropDownList, name);
        sb.AppendLine();
        sb.AppendLine("</script>");

        return sb.ToString();

    }
}

public class CascadingSelectList
{
    private IEnumerable _items;
    private string _dataKeyField;
    private string _dataValueField;
    private string _dataTextField;

    public CascadingSelectList(IEnumerable items, string dataKeyField, string dataValueField, string dataTextField)
    {
        _items = items;
        _dataKeyField = dataKeyField;
        _dataValueField = dataValueField;
        _dataTextField = dataTextField;
    }

    public List<CascadingListItem> GetListItems()
    {
        var listItems = new List<CascadingListItem>();
        foreach (var item in _items)
        {
            var key = DataBinder.GetPropertyValue(item, _dataKeyField).ToString();
            var value = DataBinder.GetPropertyValue(item, _dataValueField).ToString();
            var text = DataBinder.GetPropertyValue(item, _dataTextField).ToString();
            listItems.Add(new CascadingListItem(key, value, text));
        }
        return listItems;
    }
}

public class CascadingListItem
{
    public CascadingListItem(string key, string value, string text)
    {
        this.Key = key;
        this.Value = value;
        this.Text = text;
    }

    public string Key { get; set; }
    public string Value { get; set; }
    public string Text { get; set; }
}
问题回答

You should register the control during the application initialization. It s what you have to render in the page via CascadingDropDownList extension method.

Sys.Application.add_init(function() {
    $create(NameSpace.ClassName, null, null, null, $get("id"));
});

Type.registerNamespace("NameSpace");

NameSpace.ClassName = function(element) {
    NameSpace.ClassName.initializeBase(this, [element]);
}

NameSpace.ClassName.prototype = {

    initialize: function() {
        NameSpace.ClassName.callBaseMethod(this, "initialize");
        $addHandler(this.get_element(), "change", Function.createDelegate(this, onChange));
    },

    dispose: function() {
        NameSpace.ClassName.callBaseMethod(this, "dispose");
        $removeHandler(this.get_element(), "change", Function.createDelegate(this, onChange));
    },

    onChange: function() {
        // Do somethings...
    }

}

NameSpace.ClassName.registerClass(NameSpace.ClassName, Sys.UI.Control);

The above code snippet illustrates how to add an handler for change event.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签