开发者

ASP.Net: can onload event apply to a control

I have some code like this:

return helper.DropDownList(data.ModelEntityId.ToString(), selectList, null, new { onchange = onChange });

Where the onChange variable can be nothing, or loaded from some metadata. This works, however I also want to apply the javascript function to an onload event for that control also.

Is there a way way to apply an onload event to a control? I have tried the following, and even though there are no errors, it does not work.

 return helper.DropDownList(data.ModelEntityId.ToString(), selectList, null, new { onchange = onChange, onload = onChange });

Googling about I can only find references the to onload event applying to the whole view. This will not work for me as any javascript I am calling applies to the individual data items on the 开发者_如何学Goview rather than the whole view itself.


Why don't you add javascript code to your view like (jQuery)

$(document).ready(function() { /* process items in your dropdownlist */ });

MVC doesn't have some hook for a control's onLoad event. You attach your javascript events to the document.ready event, not on the object.


After some trial and error, I have put together the following code:

function pageLoad() {
            var script;
            <%foreach(var item in Model.Record) { %>
                <%if (item.Value.ModelEntity.ModelItem.ModelCustomOnChange != null) { %>
                    script = <%=item.Value.ModelEntity.ModelItem.ModelCustomOnChange.OnChangeScript%>;

                    if (eval('typeof(' + script + ')') == 'function') {
                        eval(script + '("' + parm1 + '", "' + parm2 + '");');
                    }
                    else {
                        alert('JavaScript function "' + script + '" could not be found.  No attempt was made to call the function.');
                    }                            
                 <% } %>
             <% } %>    
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜